Skip to contents

This function inserts a checkbox group

Usage

checkbox_Input(
  inputId,
  cb_labels,
  checkboxIds,
  label,
  hint_label = NULL,
  small = FALSE,
  error = FALSE,
  error_message = NULL,
  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>.

cb_labels

Add the names of the options that will appear

checkboxIds

Add the values for each checkbox

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().

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.

small

change the sizing to a small version of the checkbox. Defaults to FALSE

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.

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

a checkbox 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 checkbox
    shinyGovstyle::checkbox_Input(
      inputId = "check1",
      cb_labels = c("Option 1", "Option 2", "Option 3"),
      checkboxIds = c("op1", "op2", "op3"),
      label = "Choice option"
    ),
    # Error checkbox
    shinyGovstyle::checkbox_Input(
      inputId = "check2",
      cb_labels = c("Option 1", "Option 2", "Option 3"),
      checkboxIds = c("op1", "op2", "op3"),
      label = "Choice option",
      hint_label = "Select the best fit",
      error = TRUE,
      error_message = "Select one"
    ),
    # Rich content: a link in the hint
    shinyGovstyle::checkbox_Input(
      inputId = "check3",
      cb_labels = c("Option 1", "Option 2", "Option 3"),
      checkboxIds = c("op1", "op2", "op3"),
      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$check2)){
      shinyGovstyle::error_on(inputId = "check2")
    } else {
      shinyGovstyle::error_off(inputId = "check2")
    }
  })
}

if (interactive()) shinyApp(ui = ui, server = server)