Are observers triggered when the last checkbox is unchecked in a shiny widget? Here I have some checkboxes that are checked, through an observer, when the user selects some other checkbox options. However, I am finding that the observer doesn't fire when the last checkbox becomes unchecked. It works fine for everything else.
Here is an example, where the 'input' checkboxes are checked according to the 'subset' options. I added some print output for debugging, and if the subset boxes are checked, and then all of them unchecked, there is no printed output, which suggests to me that the observer hasn't worked.
library(shiny)
shinyApp(
shinyUI(
fluidPage(
uiOutput('ui')
)
),
shinyServer(function(session, input, output) {
## Some sample data
dat <- data.frame(foo=1:10, bar=factor(c('a','b')))
## The UI
output$ui <- renderUI({
inputPanel(
checkboxGroupInput('input', 'Input:', choices=dat$foo),
checkboxGroupInput('subset', 'Subset:', choices=levels(dat$bar))
)
})
## Observer for checking 'input' boxes
observeEvent(input$subset, {
## Print output for debugging
print(dat$foo[dat$bar %in% input$subset])
updateCheckboxGroupInput(session, inputId='input',
selected=paste(dat$foo[dat$bar %in% input$subset]))
})
})
)
I just want to have all the 'input' checkboxes become unchecked when all of the 'subset' checkoxes are unchecked. How to do this? Thanks!
You want to use the ignoreNull
argument of the observer. Here's a simplified example
library(shiny)
shinyApp(
shinyUI(
fluidPage(
uiOutput('ui')
)
),
shinyServer(function(session, input, output) {
output$ui <- renderUI({
inputPanel(
checkboxGroupInput('subset', 'Subset:', choices = c("a", "b"))
)
})
observeEvent(input$subset, {
print("hello: ")
print(input$subset)
}, ignoreNULL = FALSE)
})
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With