1

input$some_selection启动后发生变化时,只有第一个观察者会失效。这似乎是因为在启动时,反应值input_some_selection被设置为NULL并且不再更新。

按下单选按钮时,输出如下所示:

[1] "observed at point 1"
[1] "observed at point 2" # as expected until here
[1] "observed at point 1"
# why not invalidate the second observer also?
[1] "observed at point 1" 
[1] "observed at point 1"
[1] "observed at point 1"

1)为什么会出现这种行为?是因为初始NULL值吗?

2)我可以做些什么来使示例工作(使第二个观察者在input$some_selection更改时无效,即生成依赖项)?

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      radioButtons(inputId = "some_selection", 
                   "Distribution type:",
                   c("Normal" = "norm",
                     "Uniform" = "unif",
                     "Log-normal" = "lnorm"))
    )
  )
)

server <- function(input, output, session) {

  eventlog<-c("")

  input_some_selection <- reactive({
    input$some_selection
  })

  observeEvent(input$some_selection, {
    print("observed at point 1")
    eventlog<<-c(eventlog,"observed at point 1")
  } )

  observeEvent(input_some_selection, {
    print("observed at point 2")
    eventlog<<-c(eventlog,"observed at point 2")
  } )

}

shinyApp(ui = ui, server = server)
4

1 回答 1

1

只是一个小错字。反应值当然必须通过函数调用来访问:

observeEvent(input_some_selection(), {
    print("observed at point 2")
    eventlog<<-c(eventlog,"observed at point 2")
  } ) 

https://shiny.rstudio.com/reference/shiny/latest/reactive.html

现在按预期输出:

[1] "observed at point 1"
[1] "observed at point 2"
[1] "observed at point 1"
[1] "observed at point 2"
# ...
于 2017-12-28T17:22:01.537 回答