简体   繁体   中英

How to trigger an observeEvent in shiny when multiple selectizeInput is cleared

I would like for an observeEvent to trigger when the list of inputs is cleared.

Here is a little example app:

library(shiny)

ui <- fluidPage(

  selectizeInput(inputId='select',
                 label='Select something',
                 multiple=TRUE,
                 choices=letters[1:3])
)

server <- function(input, output){

  observeEvent(input$select, {
    print(input$select)
  })
}

shinyApp(ui=ui, server=server)

Now each time I select another letter the observeEvent is triggered. Also removing a letter triggers the event. However, removing the final letter does not seem to trigger anything.

I tried the solution here but it does not seem to apply to my case (or I do not manage to get it to work).

So my expected output would be that after selecting a it prints "a" , but then after deleting a , that it prints something as well (be it NULL or "" ).

You can use observe instead of observeEvent.

library(shiny)

ui <- fluidPage(

  selectizeInput(inputId='select',
                 label='Select something',
                 multiple=TRUE,
                 choices=letters[1:3])
)

server <- function(input, output){

  observe({
    print(input$select)
  })
}

shinyApp(ui=ui, server=server)

observeEvent has a number of arguments

observeEvent(eventExpr, handlerExpr, event.env = parent.frame(), event.quoted = FALSE, handler.env = parent.frame(), handler.quoted = FALSE, label = NULL, suspended = FALSE, priority = 0, domain = getDefaultReactiveDomain(), autoDestroy = TRUE, ignoreNULL = TRUE, ignoreInit = FALSE, once = FALSE)

including ignoreNULL , which is set to TRUE by default, simply change that to FALSE

  observeEvent(input$select, {
    print(input$select)
  },ignoreNULL  = F)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM