闪亮的selectInput不会取消选择所有元素

Bat*_*hek 4 r shiny

如何观察取消选择selectInputin中的所有元素shiny

例如

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)
Run Code Online (Sandbox Code Playgroud)

动作:

1)选择1

2)选择2

3)取消选择2

4)取消选择1

控制台日志:

[1] "1"
[1] "1" "2"
[1] "1"
Run Code Online (Sandbox Code Playgroud)

因此,取消所有选择时将没有打印。

这是错误还是我以错误的方式做某事?

Edu*_*gel 5

watchEvent不会对NULL做出反应。在大多数情况下这很有用,请参阅此问题,@ daattali的答案。

您有两种选择,1)使用观察

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)
Run Code Online (Sandbox Code Playgroud)

2)根据@WeihuangWong的建议,在observeEvent()中将ignoreNULL参数设置为FALSE

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)
Run Code Online (Sandbox Code Playgroud)

  • 为了增加答案,您还可以将`ignoreNULL`参数设置为`FALSE`以得到相同的行为,即`observeEvent(input $ select,{print(input $ select)},ignoreNULL = FALSE)`。 (4认同)