Shiny observer respond to all checkboxes being unchecked

旧时模样 提交于 2020-01-13 08:59:10

问题


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!


回答1:


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

)


来源:https://stackoverflow.com/questions/33048189/shiny-observer-respond-to-all-checkboxes-being-unchecked

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!