1
votes

I'm setting up a web application with shiny dashboard in order to display data from google analytics. I would like to let the user choose in which plot he wants to display the data by a form (plot1, plot2, plot3). I want to retrieve the location (input$select_plot) and generate the data in the selected plot when i click on action button (something like output$(input$select_plot)) :

My form

I tried different ways (with reactive) but still don't work, i don't know if it is even possible

Here is my code :

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"))
              )
            )

and server.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()) 
  })

If the user choose "Choice 3", I expect to generate a plot in plotOutput("plot3"), Is there a solution or another way to do this ?

Thks

1

1 Answers

1
votes

Here's a sample app that allows you to choose which plot.

Tricks:

  • the data doesn't change, so no need to duplicate data
  • the plots depend solely on a counter (similar to how actionButton behaves in reactivity), so whenever that counter changes, it updates its plot
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)