Make a list for the brilliant SelectInput dropdown

brilliant selectInput widget requires a named list of options in this format:

choices = list( "mpg" = 1, "cyl" = 2, "disp" = 3, "hp" = 4 # ..... etc ) 

data frames going to my brilliant application will not have the same variable names, so I would like to generate a list of names on the fly.

here is an attempt:

 data(mtcars) choices = data.frame( var = names(mtcars), num = 1:length(names(mtcars)) ) > head(choices) var num mylist 1 mpg 1 "mpg" = 1 2 cyl 2 "cyl" = 2 3 disp 3 "disp" = 3 4 hp 4 "hp" = 4 5 drat 5 "drat" = 5 6 wt 6 "wt" = 6 paste(choices$mylist, collapse = ",") 

this looks close, but it does not work:

 ... box( selectInput( "select", label = h3("Select box"), choices = list( paste(choices$mylist, collapse = ",") ) ) ... 

enter image description here

how can i do this work?

+6
source share
1 answer

Hi, if I understood your question correctly, you could just do this:

 data(mtcars) choices = data.frame( var = names(mtcars), num = 1:length(names(mtcars)) ) # List of choices for selectInput mylist <- as.list(choices$num) # Name it names(mylist) <- choices$var # ui ui <- fluidPage( selectInput( "select", label = h3("Select box"), choices = mylist ) ) # server server <- function(input, output) { } # app shinyApp(ui = ui, server = server) 
+3
source

All Articles