Select (Single)
#| 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")
),
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")
),
textOutput("value")
)
server <- function(input, output) {
output$value <- renderText({input$select})
}
shinyApp(ui = ui, server = server)
Relevant Functions
-
selectInput
selectInput( inputId, label, choices, selected = NULL, multiple = FALSE, selectize = TRUE, width = NULL, size = NULL)
Details
A select list creates a way to select one or more items from a list.
To use a select list that allows you to select a single item:
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.Specify the
inputId
andlabel
parameters ofselectInput()
to define the identifier and label of the select list.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.
The value of an input component is accessible as a reactive value within the server()
function. To access the value of a select list:
- Use
input$<select_id>
(e.g.,input$select
) to access the selected value. The server value of a select list is a vector of strings. Whenmultiple = FALSE
, this vector will have length 1.
See also
- Selectize (Single). Select inputs and selectize inputs are similar, but have different interfaces and provide different ways of selecting multiple options. Selectize inputs also allow you to deselect items.
- Select (Multiple) and Selectize (Multiple)
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 names as group labels.
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"))
),
textOutput("value")
)
server <- function(input, output) {
output$value <- renderText({input$select})
}
shinyApp(ui = ui, server = server)