Shiny: Clearing all checkboxes in checkboxGroupInput doesn't qualify as an event for observeEvent

Created on 8 Dec 2017  路  5Comments  路  Source: rstudio/shiny

When unselecting all checkboxes of a checkboxGroupInput, observeEvent does not respond to the input change but observe does. I think it would be logical that observeEvent react to the fact that the input was cleared and for consistency with observe as well.

Here is a minimal working example of what I found.

library(shiny)

ui <- fluidPage(
  fluidRow(
    checkboxGroupInput('shinyCheckBox',
                       label = NULL,
                       c('One',
                         'Two',
                         'Three'))
  )
)

server <- function(input, output, session){

  observeEvent(input$shinyCheckBox, {
      print('Shiny Checkbox observeEvent changed')
  })

  observe({
    a <- input$shinyCheckBox
    print('Shiny Checkbox observe changed')
  })

}

shinyApp(ui = ui, server = server)

Hope it helps !

Most helpful comment

You need to include ignoreNULL=FALSE in the observeEvent statement because checkboxGroupInput is NULL when no choices are selected.
```{r}
library(shiny)

ui <- fluidPage(
fluidRow(
checkboxGroupInput('shinyCheckBox',
label = NULL,
c('One',
'Two',
'Three'))
)
)

server <- function(input, output, session){

observeEvent(input$shinyCheckBox, {
print('Shiny Checkbox observeEvent changed')
}, ignoreNULL=FALSE)

observe({
a <- input$shinyCheckBox
print('Shiny Checkbox observe changed')
})

}

shinyApp(ui = ui, server = server)
```

All 5 comments

You need to include ignoreNULL=FALSE in the observeEvent statement because checkboxGroupInput is NULL when no choices are selected.
```{r}
library(shiny)

ui <- fluidPage(
fluidRow(
checkboxGroupInput('shinyCheckBox',
label = NULL,
c('One',
'Two',
'Three'))
)
)

server <- function(input, output, session){

observeEvent(input$shinyCheckBox, {
print('Shiny Checkbox observeEvent changed')
}, ignoreNULL=FALSE)

observe({
a <- input$shinyCheckBox
print('Shiny Checkbox observe changed')
})

}

shinyApp(ui = ui, server = server)
```

Thanks for the precision !

I didn't know observeEvent had options like this.
But shouldn't checkboxGroupInput be an empty list instead of NULL then ?
To me it makes no sense that observeEvent and observe behave differently in this situation.

@EmileArseneault an empty vector IS the same as NULL. Try this: identical(c(), NULL)

You're noticing a little-known feature of observeEvent. It may be weird, but it's actually useful that they chose this as the default behaviour. Before observeEvent came into existence, a lot of my observers had if(is.null(input$x)) return() because I had to manually check if the value is null. Thanks for observeEvent and its default value of ignoreNULL, that's not required anymore.

For future reference: it should probably give character(0) instead of NULL.

observeEvent actually uses req and req(input$checkboxes) should probably mean "one of these checkboxes should be checked"? I can see how it feels like a slight misfit for observeEvent though.

Was this page helpful?
0 / 5 - 0 ratings