Simon.S.A.
Simon.S.A.

Reputation: 6931

only first updateCheckboxGroups works in observer

I have two checkboxGroupInputs and two buttons.

If I swap the order of the updateCheckboxGroupInputs in the listener for the "None" button then the other checkbox-group does not clear.

What I would like is a single button/listener to clear both groups of checkboxes.

library(shiny)

# Define UI
ui <- fluidPage(
  actionButton("all_button", "All"),
  actionButton("none_button", "None"),

  checkboxGroupInput("A_checkbox", label = "A", choices = c('a','b','c')),
  checkboxGroupInput("Z_checkbox", label = "Z", choices = c('x','y','z'))
)

# Define server logic
server <- function(input, output, server, session) {

  observeEvent(input$all_button,{
    # both update
    updateCheckboxGroupInput(session, "A_checkbox", selected = c('a','b','c'))
    updateCheckboxGroupInput(session, "Z_checkbox", selected = c('x','y','z'))
  })

  observeEvent(input$none_button,{
    # the second one does not update
    updateCheckboxGroupInput(session, "A_checkbox", selected = NA)
    updateCheckboxGroupInput(session, "Z_checkbox", selected = NA)
  })
}

# Run the app
shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 153

Answers (1)

tasasaki
tasasaki

Reputation: 694

You can not use NA but character(0) to deselect checkbox group input.

observeEvent(input$none_button,{
  updateCheckboxGroupInput(session, "A_checkbox", selected = character(0))
  updateCheckboxGroupInput(session, "Z_checkbox", selected = character(0))
})

Please type ?updateCheckboxGroupInput in your console and see the help.

Upvotes: 2

Related Questions