Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny observer respond to all checkboxes being unchecked

Tags:

r

shiny

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!

like image 675
Rorschach Avatar asked Oct 09 '15 22:10

Rorschach


1 Answers

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)
  })

)
like image 151
DeanAttali Avatar answered Nov 11 '22 18:11

DeanAttali