启动后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)
答案 0 :(得分: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"
# ...