Batanichek
Batanichek

Reputation: 7871

shiny selectInput does not react to deselect all elements

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?

Upvotes: 6

Views: 1815

Answers (1)

Eduardo Bergel
Eduardo Bergel

Reputation: 2775

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)

Upvotes: 7

Related Questions