Select (Multiple)

#| standalone: true
#| components: [viewer]
#| viewerHeight: 200

library(shiny)
library(bslib)

ui <- page_fluid(
  selectInput( 
    "select", 
    "Select options below:", 
    list("Choice 1A" = "1A", "Choice 1B" = "1B", "Choice 1C" = "1C"), 
    multiple = TRUE 
  ), 
  textOutput("value")
)

server <- function(input, output) {
  output$value <- renderText({input$select})
}

shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)

ui <- page_fluid(
  selectInput( 
    "select", 
    "Select options below:", 
    list("Choice 1A" = "1A", "Choice 1B" = "1B", "Choice 1C" = "1C"), 
    multiple = TRUE 
  ), 
  textOutput("value")
)

server <- function(input, output) {
  output$value <- renderText({input$select})
}

shinyApp(ui = ui, server = server)
No matching items

Relevant Functions

  • selectInput
    selectInput(inputId, label, choices, selected = NULL, multiple = FALSE, selectize = TRUE, width = NULL, size = NULL)

No matching items

Details

A select list creates a way to select one or more items from a list.

To add a select list that allows you to select multiple items to your app:

  1. Add selectInput() to the UI of your app to create a select list. Where you call this function will determine where the select list appears within the app’s layout.

  2. Specify the inputId and label parameters of selectInput() to define the identifier and label of the select list.

  3. Supply the choices parameter with a list of choices. If the list contains names, selectInput() will display these names in place of the values within the widget’s drop down menu.

You can also supply with `choices` a list of lists. `selectInput()` will use the top-level keys as group labels. See <em>Select with grouped choices</em> variation.
  1. Set the multiple parameter to TRUE to allow the user to select multiple items at once. By default, multiple is FALSE.

The value of an input component is accessible as a reactive value within the server() function. To access the value of a select list:

  1. Use input$<select_id> (e.g., input$select) to access the selected value(s). The server value of a select list is a vector of strings.

See also

Variations

Select list with grouped choices

To group the choices into categories, supply the choices argument with a list of lists. selectInput() will use the top-level keys as group labels.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(shiny)
library(bslib)

ui <- page_fluid(
  selectInput("select",
              "Select options below:",
              list("1" = list("Choice 1A" = "1A", 
                              "Choice 1B" = "1B", 
                              "Choice 1C" = "1C"), 
                   "2" = list("Choice 2A" = "2A", 
                              "Choice 2B" = "2B", 
                              "Choice 2C" = "2C")), 
              multiple = TRUE
  ),
  textOutput("value")
)

server <- function(input, output) {
  output$value <- renderText({input$select})
}

shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)

ui <- page_fluid(
  selectInput("select",
              "Select options below:",
              list("1" = list("Choice 1A" = "1A", 
                              "Choice 1B" = "1B", 
                              "Choice 1C" = "1C"), 
                   "2" = list("Choice 2A" = "2A", 
                              "Choice 2B" = "2B", 
                              "Choice 2C" = "2C")), 
              multiple = TRUE
  ),
  textOutput("value")
)

server <- function(input, output) {
  output$value <- renderText({input$select})
}

shinyApp(ui = ui, server = server)
No matching items