Select variables from a data frame — varSelectInput

v1.7.0|Source: R/input-select.R

Description

Create a select list that can be used to choose a single or multiple items from the column names of a data frame.

varSelectInput(
  inputId,
  label,
  data,
  selected = NULL,
  multiple = FALSE,
  selectize = TRUE,
  width = NULL,
  size = NULL
)

varSelectizeInput(inputId, ..., options = NULL, width = NULL)

Arguments

inputId

The input slot that will be used to access the value.

label

Display label for the control, or NULL for no label.

data

A data frame. Used to retrieve the column names as choices for a selectInput()

selected

The initially selected value (or multiple values if multiple = TRUE). If not specified then defaults to the first value for single-select lists and no values for multiple select lists.

multiple

Is selection of multiple items allowed?

selectize

Whether to use selectize.js or not.

width

The width of the input, e.g. '400px', or '100%'; see validateCssUnit().

size

Number of items to show in the selection box; a larger number will result in a taller box. Not compatible with selectize=TRUE. Normally, when multiple=FALSE, a select input will be a drop-down list, but when size is set, it will be a box instead.

...

Arguments passed to varSelectInput().

options

A list of options. See the documentation of selectize.js for possible options (character option values inside base::I() will be treated as literal JavaScript code; see renderDataTable() for details).

Value

A variable select list control that can be added to a UI definition.

Details

By default, varSelectInput() and selectizeInput() use the JavaScript library selectize.js (https://github.com/selectize/selectize.js) to instead of the basic select input element. To use the standard HTML select input element, use selectInput() with selectize=FALSE.

Note

The variable selectize input created from varSelectizeInput() allows deletion of the selected option even in a single select input, which will return an empty string as its value. This is the default behavior of selectize.js. However, the selectize input created from selectInput(..., selectize = TRUE) will ignore the empty string value when it is a single choice input and the empty string is not in the choices argument. This is to keep compatibility with selectInput(..., selectize = FALSE).

Server value

The resulting server input value will be returned as:

  • A symbol if multiple = FALSE. The input value should be used with rlang's rlang::!!(). For example, ggplot2::aes(!!input$variable).

  • A list of symbols if multiple = TRUE. The input value should be used with rlang's rlang::!!!() to expand the symbol list as individual arguments. For example, dplyr::select(mtcars, !!!input$variabls) which is equivalent to dplyr::select(mtcars, !!input$variabls[[1]], !!input$variabls[[2]], ..., !!input$variabls[[length(input$variabls)]]).

See also

Examples


## Only run examples in interactive R sessions
if (interactive()) {

library(ggplot2)

# single selection
shinyApp(
  ui = fluidPage(
    varSelectInput("variable", "Variable:", mtcars),
    plotOutput("data")
  ),
  server = function(input, output) {
    output$data <- renderPlot({
      ggplot(mtcars, aes(!!input$variable)) + geom_histogram()
    })
  }
)


# multiple selections
if (FALSE) {
shinyApp(
 ui = fluidPage(
   varSelectInput("variables", "Variable:", mtcars, multiple = TRUE),
   tableOutput("data")
 ),
 server = function(input, output) {
   output$data <- renderTable({
      if (length(input$variables) == 0) return(mtcars)
      mtcars %>% dplyr::select(!!!input$variables)
   }, rownames = TRUE)
 }
)}

}