Null checkboxes in Shiny

Author
Published

September 20, 2024

This was an idea I had

ui <- fluidPage(
  checkboxGroupInput(
    "checks",
    "Check boxes",
    choices = month.name,
    selected = month.name[1]
  ),
  actionButton("search", "Search"),
  uiOutput("ui_stuff")
)

server <- function(input, output, session) {
  reactive_checkboxes <- reactiveValues(checks = c())

  observeEvent(input$checks, {
    print("change")

    reactive_checkboxes$checks <- input$checks
  })

  observeEvent(input$search, {
    if (is.null(input$checks)) {
      showModal(modalDialog(
        title = "Invalid search conditions!",
        div(p("You must select at least one theme to filter the data."),
            p("Click dismiss and your previously selected set of themes will be re-selected.")
      )))
      
      updateCheckboxGroupInput(session,
                               "checks",
                               selected = reactive_checkboxes$checks)
      
    }
  })


  output$ui_stuff <- renderUI({
    "dummy element here"
  })
}


shinyApp(ui, server)

Reuse

Citation

BibTeX citation:
@online{hadley2024,
  author = {Hadley, Charlie},
  title = {Null Checkboxes in {Shiny}},
  date = {2024-09-20},
  url = {https://visibledata.co.uk/posts/2024-02-27_empty-checkboxes},
  langid = {en}
}
For attribution, please cite this work as:
Hadley, Charlie. 2024. “Null Checkboxes in Shiny.” September 20, 2024. https://visibledata.co.uk/posts/2024-02-27_empty-checkboxes.