Home > OS >  How to have pickerInput initialize with all choices selected from a list
How to have pickerInput initialize with all choices selected from a list

Time:05-10

pickerInput can be made to initialize with all choices selected by passing the choice list into the selected box as follows:

library(shiny)

choice_list <- c("Tom", "Hank", "Kelly")

ui <- fluidPage(
  pickerInput(inputId = "grp_sel",
              label = "Select Groups",
              choices = choice_list,
              selected = choice_list,
              multiple = T)
)

server <- function(input, output, session) {
  
}

shinyApp(ui, server)

However, I would like to do this with a list of choices as follows but this is not working. Can anyone tell me what a solution is to this?

choice_list <- list("Group A" = c("Tom", "Jenny", "Kim"),
                    "Group B" = c("Hank", "James", "Kelly"))

ui <- fluidPage(
  pickerInput(inputId = "grp_sel",
              label = "Select Groups",
              choices = choice_list,
              selected = choice_list,
              multiple = T)
)

server <- function(input, output, session) {
  
}

shinyApp(ui, server)

CodePudding user response:

If you pass a vector instead of a list all choices will be selected: selected = unlist(choice_list)

  • Related