Debian Codegraph @

R/input-radiobuttons.R

raw
1 #' Create radio buttons
2 #'
3 #' Create a set of radio buttons used to select an item from a list.
4 #'
5 #' If you need to represent a "None selected" state, it's possible to default
6 #' the radio buttons to have no options selected by using `selected =
7 #' character(0)`. However, this is not recommended, as it gives the user no way
8 #' to return to that state once they've made a selection. Instead, consider
9 #' having the first of your choices be `c("None selected" = "")`.
10 #'
11 #' @inheritParams textInput
12 #' @param choices List of values to select from (if elements of the list are
13 #' named then that name rather than the value is displayed to the user). If
14 #' this argument is provided, then `choiceNames` and `choiceValues` must not
15 #' be provided, and vice-versa. The values should be strings; other types
16 #' (such as logicals and numbers) will be coerced to strings.
17 #' @param selected The initially selected value. If not specified, then it
18 #' defaults to the first item in `choices`. To start with no items selected,
19 #' use `character(0)`.
20 #' @param inline If `TRUE`, render the choices inline (i.e. horizontally)
21 #' @return A set of radio buttons that can be added to a UI definition.
22 #' @param choiceNames,choiceValues List of names and values, respectively, that
23 #' are displayed to the user in the app and correspond to the each choice (for
24 #' this reason, `choiceNames` and `choiceValues` must have the same length).
25 #' If either of these arguments is provided, then the other *must* be provided
26 #' and `choices` *must not* be provided. The advantage of using both of these
27 #' over a named list for `choices` is that `choiceNames` allows any type of UI
28 #' object to be passed through (tag objects, icons, HTML code, ...), instead
29 #' of just simple text. See Examples.
30 #'
31 #' @family input elements
32 #' @seealso [updateRadioButtons()]
33 #'
34 #' @examples
35 #' ## Only run examples in interactive R sessions
36 #' if (interactive()) {
37 #'
38 #' ui <- fluidPage(
39 #' radioButtons("dist", "Distribution type:",
40 #' c("Normal" = "norm",
41 #' "Uniform" = "unif",
42 #' "Log-normal" = "lnorm",
43 #' "Exponential" = "exp")),
44 #' plotOutput("distPlot")
45 #' )
46 #'
47 #' server <- function(input, output) {
48 #' output$distPlot <- renderPlot({
49 #' dist <- switch(input$dist,
50 #' norm = rnorm,
51 #' unif = runif,
52 #' lnorm = rlnorm,
53 #' exp = rexp,
54 #' rnorm)
55 #'
56 #' hist(dist(500))
57 #' })
58 #' }
59 #'
60 #' shinyApp(ui, server)
61 #'
62 #' ui <- fluidPage(
63 #' radioButtons("rb", "Choose one:",
64 #' choiceNames = list(
65 #' icon("calendar"),
66 #' HTML("<p style='color:red;'>Red Text</p>"),
67 #' "Normal text"
68 #' ),
69 #' choiceValues = list(
70 #' "icon", "html", "text"
71 #' )),
72 #' textOutput("txt")
73 #' )
74 #'
75 #' server <- function(input, output) {
76 #' output$txt <- renderText({
77 #' paste("You chose", input$rb)
78 #' })
79 #' }
80 #'
81 #' shinyApp(ui, server)
82 #' }
83 #'
84 #' @section Server value:
85 #'
86 #' A character string containing the value of the selected button.
87 #'
88 #' @export
89 radioButtons <- function(inputId, label, choices = NULL, selected = NULL,
90 inline = FALSE, width = NULL, choiceNames = NULL, choiceValues = NULL) {
91
92 args <- normalizeChoicesArgs(choices, choiceNames, choiceValues)
93
94 selected <- restoreInput(id = inputId, default = selected)
95
96 # default value if it's not specified
97 selected <- if (is.null(selected)) args$choiceValues[[1]] else as.character(selected)
98
99 if (length(selected) > 1) stop("The 'selected' argument must be of length 1")
100
101 options <- generateOptions(inputId, selected, inline,
102 'radio', args$choiceNames, args$choiceValues)
103
104 divClass <- "form-group shiny-input-radiogroup shiny-input-container"
105 if (inline) divClass <- paste(divClass, "shiny-input-container-inline")
106
107 inputLabel <- shinyInputLabel(inputId, label)
108 tags$div(id = inputId,
109 style = css(width = validateCssUnit(width)),
110 class = divClass,
111 # https://www.w3.org/TR/2017/WD-wai-aria-practices-1.1-20170628/examples/radio/radio-1/radio-1.html
112 role = "radiogroup",
113 `aria-labelledby` = inputLabel$attribs$id,
114 inputLabel,
115 options
116 )
117 }