I've been trying to extend the second solution posted by Ramnath (the one using updateSelectInput
) in the problem originally posted at R shiny passing reactive to selectInput choices . I'd like to allow the user to select inputs from a hierarchical series of inputs where the choice in each selection updates the possible values in the next selection. As an example, in the following I modify Ramnath's code by simply adding a third input that lists the values of the variable selected in the second input. This example is based on the mtcars
and iris
datasets, and was run on R v 3.1.1 and RStudio v 0.98.1062. It doesn't throw an error but you'll see that I'm stuck on how to add a second reactive()
that takes as its input a combination of input$dataset
and input$column
.
library(shiny)
runApp(list(
ui = bootstrapPage(
selectInput('dataset', 'Choose data set', c('mtcars', 'iris')),
selectInput('columns', 'Choose variable', ""),
selectInput('values', 'Show values', "")
),
server = function(input, output, session){
# updates variable names based on selected dataset
outVar = reactive({
names(get(input$dataset))
})
# i want this to update the values of the selected variable
outVar2 = reactive({
sort(unique(get(input$dataset)[, 2])) # this works but of course I don't want the second variable every time
#sort(unique(get(input$dataset)[, input$columns])) # this fails but this is the idea I'm after
})
# i want these to update the UI based on the reactive output above
observe({
updateSelectInput(session, "columns", choices = outVar())
updateSelectInput(session, "values", choices = outVar2())
})
}
))
selectInput
in arenderUI
on the server side. – Alex