6

How to observe of deselecte all elements of selectInput in shiny?

for example

library(shiny)

ui=shinyUI(fluidPage(
  selectInput("select","",choices = c(1,2),multiple = T)
  ))

server=function(input, output,session) {
  observeEvent(input$select,{
    print(input$select)
  })

}

shinyApp(ui,server)

actions :

1) select 1

2) select 2

3) deselect 2

4) deselect 1

console log:

[1] "1"
[1] "1" "2"
[1] "1"

So there is no print when all deselected.

It is bug or i do something in wrong way?

Eduardo Bergel
  • 2,685
  • 1
  • 16
  • 21
Batanichek
  • 7,761
  • 31
  • 49

1 Answers1

7

observeEvent does not react to NULL. This is usefull in most cases, See this question , the answer by @daattali.

You have two options, 1) use observe

library(shiny)

ui=shinyUI(fluidPage(
  selectInput("select","",choices = c(1,2),multiple = T)
  ))

server=function(input, output,session) {
  observe({
    print(input$select)
  })

}

shinyApp(ui,server)

2) set the ignoreNULL parameter to FALSE in observeEvent(), as suggested by @WeihuangWong

library(shiny)

ui=shinyUI(fluidPage(
  selectInput("select","",choices = c(1,2),multiple = T)
))

server=function(input, output,session) {
  observeEvent(input$select,{
    print(input$select)
  }, ignoreNULL = FALSE) 
}

shinyApp(ui,server)
Community
  • 1
  • 1
Eduardo Bergel
  • 2,685
  • 1
  • 16
  • 21
  • 4
    To add to the answer, you can also set the `ignoreNULL` parameter to `FALSE` to get the same behavior, i.e. `observeEvent(input$select,{print(input$select)}, ignoreNULL = FALSE)`. – Weihuang Wong Nov 02 '16 at 11:42
  • 1
    @WeihuangWong It better than simply observe ( my mind) because observe call multyple times in my real case , thanks – Batanichek Nov 02 '16 at 11:45
  • Good point @WeihuangWong. Will edit the answer to include your point. – Eduardo Bergel Nov 02 '16 at 11:46