| 1 |
#' Checkbox Group Input Control |
| 2 |
#' |
| 3 |
#' Create a group of checkboxes that can be used to toggle multiple choices |
| 4 |
#' independently. The server will receive the input as a character vector of the |
| 5 |
#' selected values. |
| 6 |
#' |
| 7 |
#' @inheritParams textInput |
| 8 |
#' @param choices List of values to show checkboxes for. If elements of the list |
| 9 |
#' are named then that name rather than the value is displayed to the user. If |
| 10 |
#' this argument is provided, then `choiceNames` and `choiceValues` |
| 11 |
#' must not be provided, and vice-versa. The values should be strings; other |
| 12 |
#' types (such as logicals and numbers) will be coerced to strings. |
| 13 |
#' @param selected The values that should be initially selected, if any. |
| 14 |
#' @param inline If `TRUE`, render the choices inline (i.e. horizontally) |
| 15 |
#' @param choiceNames,choiceValues List of names and values, respectively, |
| 16 |
#' that are displayed to the user in the app and correspond to the each |
| 17 |
#' choice (for this reason, `choiceNames` and `choiceValues` |
| 18 |
#' must have the same length). If either of these arguments is |
| 19 |
#' provided, then the other *must* be provided and `choices` |
| 20 |
#' *must not* be provided. The advantage of using both of these over |
| 21 |
#' a named list for `choices` is that `choiceNames` allows any |
| 22 |
#' type of UI object to be passed through (tag objects, icons, HTML code, |
| 23 |
#' ...), instead of just simple text. See Examples. |
| 24 |
#' |
| 25 |
#' @return A list of HTML elements that can be added to a UI definition. |
| 26 |
#' |
| 27 |
#' @family input elements |
| 28 |
#' @seealso [checkboxInput()], [updateCheckboxGroupInput()] |
| 29 |
#' |
| 30 |
#' @examples |
| 31 |
#' ## Only run examples in interactive R sessions |
| 32 |
#' if (interactive()) { |
| 33 |
#' |
| 34 |
#' ui <- fluidPage( |
| 35 |
#' checkboxGroupInput("variable", "Variables to show:", |
| 36 |
#' c("Cylinders" = "cyl", |
| 37 |
#' "Transmission" = "am", |
| 38 |
#' "Gears" = "gear")), |
| 39 |
#' tableOutput("data") |
| 40 |
#' ) |
| 41 |
#' |
| 42 |
#' server <- function(input, output, session) { |
| 43 |
#' output$data <- renderTable({ |
| 44 |
#' mtcars[, c("mpg", input$variable), drop = FALSE] |
| 45 |
#' }, rownames = TRUE) |
| 46 |
#' } |
| 47 |
#' |
| 48 |
#' shinyApp(ui, server) |
| 49 |
#' |
| 50 |
#' ui <- fluidPage( |
| 51 |
#' checkboxGroupInput("icons", "Choose icons:", |
| 52 |
#' choiceNames = |
| 53 |
#' list(icon("calendar"), icon("bed"), |
| 54 |
#' icon("cog"), icon("bug")), |
| 55 |
#' choiceValues = |
| 56 |
#' list("calendar", "bed", "cog", "bug") |
| 57 |
#' ), |
| 58 |
#' textOutput("txt") |
| 59 |
#' ) |
| 60 |
#' |
| 61 |
#' server <- function(input, output, session) { |
| 62 |
#' output$txt <- renderText({ |
| 63 |
#' icons <- paste(input$icons, collapse = ", ") |
| 64 |
#' paste("You chose", icons) |
| 65 |
#' }) |
| 66 |
#' } |
| 67 |
#' |
| 68 |
#' shinyApp(ui, server) |
| 69 |
#' } |
| 70 |
#' @section Server value: |
| 71 |
#' Character vector of values corresponding to the boxes that are checked. |
| 72 |
#' |
| 73 |
#' @export |
| 74 |
checkboxGroupInput <- function(inputId, label, choices = NULL, selected = NULL, |
| 75 |
inline = FALSE, width = NULL, choiceNames = NULL, choiceValues = NULL) { |
| 76 |
|
| 77 |
# keep backward compatibility with Shiny < 1.0.1 (see #1649) |
| 78 |
if (is.null(choices) && is.null(choiceNames) && is.null(choiceValues)) { |
| 79 |
choices <- character(0) |
| 80 |
} |
| 81 |
|
| 82 |
args <- normalizeChoicesArgs(choices, choiceNames, choiceValues) |
| 83 |
|
| 84 |
selected <- restoreInput(id = inputId, default = selected) |
| 85 |
|
| 86 |
# default value if it's not specified |
| 87 |
if (!is.null(selected)) selected <- as.character(selected) |
| 88 |
|
| 89 |
options <- generateOptions(inputId, selected, inline, |
| 90 |
'checkbox', args$choiceNames, args$choiceValues) |
| 91 |
|
| 92 |
divClass <- "form-group shiny-input-checkboxgroup shiny-input-container" |
| 93 |
if (inline) |
| 94 |
divClass <- paste(divClass, "shiny-input-container-inline") |
| 95 |
|
| 96 |
# return label and select tag |
| 97 |
inputLabel <- shinyInputLabel(inputId, label) |
| 98 |
tags$div(id = inputId, |
| 99 |
style = css(width = validateCssUnit(width)), |
| 100 |
class = divClass, |
| 101 |
# https://www.w3.org/TR/wai-aria-practices/examples/checkbox/checkbox-1/checkbox-1.html |
| 102 |
role = "group", |
| 103 |
`aria-labelledby` = inputLabel$attribs$id, |
| 104 |
inputLabel, |
| 105 |
options |
| 106 |
) |
| 107 |
} |