Skip to contents

This function create radio buttons

Usage

radio_button_Input(
  inputId,
  label,
  choices = NULL,
  selected = NULL,
  inline = FALSE,
  small = FALSE,
  choiceNames = NULL,
  choiceValues = NULL,
  hint_label = NULL,
  error = FALSE,
  error_message = NULL,
  custom_class = "",
  label_size = c("m", "s", "l", "xl"),
  heading_level = NULL
)

Arguments

inputId

The id assigned to the component's root element. For Shiny input components this is also the name used to access the value via input$<inputId>.

label

Display label for the control, or NULL for no label. Accepts a plain character string, an HTML string, or shiny tag objects such as shiny::tags$b("Bold") or a shiny::tagList().

choices

List of values to select from (if elements of the list are named then that name rather than the value is displayed to the user)

selected

The initially selected value.

inline

If you want the radio inline or not, Default is FALSE

small

If you want the smaller versions of radio buttons, Default is FALSE

choiceNames, choiceValues

Same as in shiny::checkboxGroupInput(). List of names and values, respectively, that are displayed to the user in the app and correspond to the each choice (for this reason they must have the same length). If either of these arguments is provided, then the other must be provided and choices must not be provided. The advantage of using both of these over a named list for choices is that choiceNames allows any type of UI object to be passed through (tag objects, icons, HTML code, ...), instead of just simple text

hint_label

Display hint label for the control, or NULL for no hint label. Accepts the same rich content as label, so it can include a link.

error

If TRUE, render the component in its error state and reserve a slot for the error message. Defaults to FALSE.

error_message

Text shown when error is TRUE. Defaults to NULL.

custom_class

If you want to add additional classes to the radio buttons

label_size

Size modifier for the legend. One of "m", "s", "l", or "xl", matching the GDS govuk-fieldset__legend--* classes. Defaults to "m".

heading_level

Optional heading level for the legend. If supplied (an integer 1-6), the legend text is wrapped in a <hN> with the GDS govuk-fieldset__heading class, following the GDS pattern for using a question as the page heading. Defaults to NULL (no heading wrap).

Value

radio buttons HTML shiny tag object

Examples

ui <- shinyGovstyle::gov_page(
  # Required for error handling function
  shinyjs::useShinyjs(),
  shinyGovstyle::header(
    org_name = "Example",
    service_name = "User Examples",
    logo = "shinyGovstyle/images/moj_logo.png"
  ),
  shinyGovstyle::banner(
    inputId = "banner", type = "beta", "This is a new service"
  ),
  shinyGovstyle::gov_layout(
    size = "two-thirds",
    # Simple radio
    shinyGovstyle::radio_button_Input(
      inputId = "radio1",
      choices = c("Yes", "No", "Maybe"),
      label = "Choice option"
    ),
    # Error radio
    shinyGovstyle::radio_button_Input(
      inputId = "radio2",
      choices = c("Yes", "No", "Maybe"),
      label = "Choice option",
      hint_label = "Select the best fit",
      inline = TRUE,
      error = TRUE,
      error_message = "Select one"
    ),
    # Rich content: a link in the hint
    shinyGovstyle::radio_button_Input(
      inputId = "radio3",
      choices = c("Yes", "No", "Maybe"),
      label = "Choice option",
      hint_label = shiny::tagList(
        "See the ",
        shinyGovstyle::external_link("https://www.gov.uk", "GOV.UK guidance")
      )
    ),
    # Button to trigger error
    shinyGovstyle::button_Input(inputId = "submit", label = "Submit")
  ),
  shinyGovstyle::footer(full = TRUE)
)

server <- function(input, output, session) {
  # Trigger error on blank submit of eventId2
  observeEvent(input$submit, {
    if (is.null(input$radio2)) {
      shinyGovstyle::error_on(inputId = "radio2")
    } else {
      shinyGovstyle::error_off(
        inputId = "radio2"
      )
    }
  })
}
if (interactive()) shinyApp(ui = ui, server = server)