0
ui <- fluidPage(
    checkboxGroupInput("data", "Select data:",
                       c("Iris" = "iris",
                         "Cars" = "mtcars")),
    plotOutput("myPlot")
  )

  server <- function(input, output) {
    output$myPlot <- renderPlot({
      plot(Sepal.Width ~ Sepal.Length, data = input$data)
    })
  }

  shinyApp(ui, server)

I have a shinyApp where I want the user to select a data set. From there, I want to use that data set to make a simple plot. However, it seems that the user input into the checkbox didn't pass in successfully to the server. How can I get around this?

Adrian
  • 9,229
  • 24
  • 74
  • 132
  • your `input$data` would return a character vector `"iris"`, not the dataset `iris`. In your plotting function, change to this `data = data(input$data)`, which will load that specified data set – Jean Mar 01 '17 at 05:43
  • @waterling thanks for the quick response. Yes, I noticed that as well. Unfortunately your suggestion did not work. I get the same error: 'invalid 'envir' argument of type 'character' – Adrian Mar 01 '17 at 05:49
  • hm ok. try `get(input$data)`. That works for me. `output$myPlot <- renderPlot({ if(is.null(input$data)) {return()} plot(Sepal.Width ~ Sepal.Length, data = get(input$data)) })` – Jean Mar 01 '17 at 05:56

1 Answers1

0

The typical way to do this in shiny is with switch(), which means you don't need to specify the dataset in your input, you can do it all in the server. In your context:

library(shiny)
ui <- fluidPage(
  checkboxGroupInput("data", "Select data:",
                     c("Iris" = "iris",
                       "Cars" = "mtcars")),
  plotOutput("myPlot")
)

server <- function(input, output) {
  dat <- reactive({
    switch()
  })
  output$myPlot <- renderPlot({
    dat <- switch(input$data, 
                  "iris" = iris,
                  "mtcars" = mtcars)
    plot(Sepal.Width ~ Sepal.Length, data = get(input$data))
  })
}

shinyApp(ui, server)

Note that you could use any strings in the checkboxGroupInput, which makes this a more flexible way to work.

alexwhan
  • 15,636
  • 5
  • 52
  • 66