【问题标题】:is it possible to choose the location of the plot with an input variable?是否可以使用输入变量选择绘图的位置?
【发布时间】:2019-10-11 05:37:44
【问题描述】:

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

My form

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

这是我的代码:

ui.R

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") 中生成一个绘图, 有解决方案或其他方法吗?

感谢

【问题讨论】:

    标签: r plot google-analytics shiny


    【解决方案1】:

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

    技巧:

    • 数据不会改变,所以不需要复制数据
    • 绘图仅依赖于计数器(类似于 actionButton 在反应性中的行为),因此每当计数器发生变化时,它都会更新其绘图
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 2019-07-07
      • 2020-07-21
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      相关资源
      最近更新 更多