selectInput() syntax
selectInput(inputId, label, choices, selected = NULL, multiple = FALSE, selectize = TRUE, width = NULL, size = NULL)
The control creates a dropdown list, but the ‘size
‘ argument can be used to set the control in a selection box.
inputID
– unique name for the control, not displayed to the user;label
– text label for the input placed above the dropdown, visible to the user;choices
– the text items that the user can select from;selected
– NULL/string; whether there is already an item pre-selected in the dropdown field;multiple
– TRUE/FALSE; can the user select more than one item from the dropdown;selectize
– TRUE/FALSE; whether to enable selective.js in the control;width
– width of the input control, in px or as a %;size
– the number of items to show in a selection box, the larger the number the taller the box. (Not compatible with selectize=TRUE.)...
– additional arguments, and selective.jsoptions
can also be included.
Add R Shiny interactive selectInput selection controls
A selectInput()
control can be placed on its own in a layout:
selectInput("choose",
label="Select",
choices=c("Red","Green","Blue"))
… or within a ‘panel’, e.g. inside an inputPanel()
layout (as shown in the document default content above). This will place the input inside a formatted panel.
inputPanel(selectInput("choose",
label="Select",
choices =c("Red","Green","Blue"))
)
The selectInput choices list
The list of choices in the selectInput control can be fed from different sources:
- a list within the function:
choices=c("A","B","C")
- a list variable defined outside of the function:
choices_list <- c("A","B","C")
selectInput("choose",
label="Select",
choices=choices_list)
- a subset of a matrix/data table, e.g. the user first selects a category, and the list of choices is taken from that category (where
table
=datatable,input$category
is the output from the user selectInput ID ‘category’, and items are the items within the chosen category).
selectInput("choose",
label="Select",
choices = unique(subset(datatable, category==input$category,
select=items)))
See more on how to create a selectInput dependent on another Shiny input control
>> On the next page we show how to use the output from a selectInput() function