闪亮的selectInput不会对取消选择所有元素做出反应。

6
如何观察或取消选择 shinyselectInput 的所有元素?
例如:
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)

操作:

1)选择 1

2)选择 2

3)取消选择 2

4)取消选择 1

控制台日志:

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

当全部取消选择时,就不会有打印输出。

这是一个Bug还是我做错了什么?

1个回答

7

observeEvent 不会对 NULL 做出反应。在大多数情况下,这是有用的。可以参考 @daattali 在 这个问题 中的回答。

你有两个选项:1)使用 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) 在observeEvent()中将ignoreNULL参数设置为FALSE,正如@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)

4
另外,为了得到相同的行为,您还可以将ignoreNULL参数设置为FALSE,即 observeEvent(input$select,{print(input$select)}, ignoreNULL = FALSE) - Weihuang Wong
1
@WeihuangWong,这比简单观察(我的思维)更好,因为在我的实际情况中,观察会被多次调用,谢谢。 - Batanichek
好观点 @WeihuangWong。会编辑答案以包含您的观点。 - Eduardo Bergel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接