Shiny中SelectizeGroup如何设置输入默认值?
Setting Default Values for selectizeGroup in shinyWidgets
Absolutely! You can set default selected values for your selectizeGroup inputs using the selected parameter in the selectizeGroupServer module. This works similarly to the selected argument in selectizeInput, but you pass a named list where each name corresponds to one of your variables, and the value is the default selection(s).
Modified Code with Default Selection
Here's how to adjust your server code to pre-select the first value of var_one when the app starts:
a_df <- tibble( var_one = c("hadley", "charlotte", "rené", "raymond"), var_two = c("mutate", "filter", "slice", "spread"), var_three = c("an apple", "a pipe", "a cocktail", "a dog"), var_four = c("with", "without", "thanks", "out of"), var_five = c("tidyr", "magrittr", "purrr", "dplyr") ) ex_df <- expand.grid(a_df) tib <- as_tibble(sample_n(ex_df, 40)) library(shinyWidgets) shinyApp( ui = pageWithSidebar( headerPanel("Painting 8"), sidebarPanel( selectizeGroupUI( id = "my-filters", inline = FALSE, params = list( var_one = list(inputId = "var_one", title = "Select variable 1", placeholder = 'select'), var_two = list(inputId = "var_two", title = "Select variable 2", placeholder = 'select'), var_three = list(inputId = "var_three", title = "Select variable 3", placeholder = 'select'), var_four = list(inputId = "var_four", title = "Select variable 4", placeholder = 'select'), var_five = list(inputId = "var_five", title = "Select variable 5", placeholder = 'select') ) ) ), mainPanel( tableOutput("table") ) ), server = function(input, output, session) { res_mod <- callModule( module = selectizeGroupServer, id = "my-filters", data = tib, vars = c("var_one", "var_two", "var_three", "var_four", "var_five"), # Add the selected parameter to set defaults selected = list(var_one = unique(tib$var_one)[1]) # Pre-select first unique value of var_one ) output$table <- renderTable({ res_mod() }) }, options = list(height = 500) )
Key Details:
- Single-select defaults: For single-select inputs (your case), pass a single value—either dynamically pull the first unique value from your data like
unique(tib$var_one)[1], or hardcode a specific value such as"hadley". - Multi-select defaults: If you enable multi-select for an input by adding
multiple = TRUEto its params entry, you can pass a vector of values (e.g.,var_two = c("mutate", "filter")) to pre-select multiple options. - No default for inputs: Simply omit variables from the
selectedlist if you want them to start with no selection (like yourvar_twotovar_fivein this example).
This approach ensures your app starts with the desired pre-selected value for var_one, while leaving other inputs empty as requested.
内容的提问来源于stack exchange,提问作者Marco Fumagalli
相关产品推荐
相关产品推荐

