r - 是否可以使用输入变量选择绘图的位置?

标签 r plot google-analytics shiny

我正在设置一个带有 Shiny 仪表板的 Web 应用程序,以显示来自谷歌分析的数据。
我想让用户选择他想通过表单(plot1,plot2,plot3)在哪个图中显示数据。
当我单击操作按钮(类似于 output$(input$select_plot))时,我想检索位置(input$select_plot)并在所选图中生成数据:

My form

我尝试了不同的方法(响应式(Reactive))但仍然不起作用,我不知道这是否可能

这是我的代码:

用户界面

tabItem("perso2", h1("Sub-item 2 tab content"),            
              hr(),
              fluidRow(
              column(3, wellPanel(
              selectInput("select2", label = h3("Metrics"),
                          multiple = TRUE,
                          choices = list("Users" = "users",
                                    "New users" = "newUsers",
                                    "Sessions" = "sessions",
                                    "Session Duration" = "sessionDuration",
                                    "Average session" = "avgSessionDuration"), 
                                    selected = 1),

                  selectInput("select_plot", label = h3("Select box"), 
                              choices = list("Choice 1" = "plot1", "Choice 2" = "plot2", "Choice 3" = "plot3")),

                  actionButton("do", "Click Me")

                ))),
              fluidRow(
              column(width = 4, plotOutput("plot1")),
              column(width = 4, plotOutput("plot2")),
              column(width = 4, plotOutput("plot3"))
              )
            )

和服务器.R

  dataaa <- eventReactive(input$do, {
    token <- auth$token()

    time <- rollupGA(GAProfileTable = getprofile(),
                     start_date = "2019-04-04",
                     end_date = "2019-04-11",
                     metrics = paste("ga:",input$select2, collapse=NULL, sep=""),
                     ga = token)
    time[,c('date',input$select2)]
  })

  selected_plot <- reactive({ input <- select_plot })

    output$selected_plot <- renderPlot({
    validate(
      need(getprofile(), "Authentificate to see"))
      plot(dataaa()) 
  })

如果用户选择“Choice 3”,我希望在 plotOutput("plot3") 中生成一个绘图,
有解决方案或其他方法吗?

谢谢

最佳答案

这是一个示例应用程序,可让您选择哪个绘图。

技巧:

  • 数据不会改变,所以不需要复制数据
  • 绘图仅取决于计数器(类似于 actionButton 在 react 性中的行为),因此每当该计数器更改时,它都会更新其绘图

  • library(shiny)
    
    myplot <- function(s) {
      w <- strwidth(s)
      plot(0, type = 'n', xlim = 0:1, ylim = 0:1, axes = FALSE, ann = FALSE)
      box()
      text(0.5, 0.5, labels = s, cex = 0.5/strwidth(s))
    }
    
    ui <- fluidPage(
      fluidRow(
        textInput("words", label = NULL, placeholder = "Something to say?"),
        selectInput("which", "Which plot?", choices = c("plot1", "plot2"), selected = "plot1"),
        actionButton("go", "Say it!")
      ),
      fluidRow(
        column(width = 4, plotOutput("plot1")),
        column(width = 4, plotOutput("plot2"))
      )
    )
    
    server <- function(input, output, session) {
    
      whichplot <- reactiveValues(plot1 = 0, plot2 = 0)
    
      observeEvent(input$go, {
        req(input$which)
        whichplot[[ input$which ]] <- whichplot[[ input$which ]] + 1
      })
    
      output$plot1 <- renderPlot({
        req(whichplot$plot1, isolate(input$words))
        message("plotting 1")
        myplot(isolate(input$words))
      })
    
      output$plot2 <- renderPlot({
        req(whichplot$plot2, isolate(input$words))
        message("plotting 2")
        myplot(isolate(input$words))
      })
    
    }
    
    shinyApp(ui, server)
    

    关于r - 是否可以使用输入变量选择绘图的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56295658/

    相关文章:

    r - 在R中创建栅格直方图

    r - 子集有向图

    matlab - MATLAB 刻度线 VS 网格的颜色

    python - matplotlib 图例先水平排列

    google-analytics - GA/GTM 增强型电子商务 |测量产品和产品细节印象

    Magento Google Analytics 代码未显示

    iphone - 具有原始数据导出功能的 iOS 免费事件跟踪

    r - 将多个列突变为一个函数,为 dplyr 中结果列内的每个组件创建一个列表

    r - 时间序列的ggplot条形图

    r - r 中绘图区域内的子图