我有一个应用程序,有两个observeEvent() 处理程序对输入A 和输入B 做出反应并执行一些操作。事件 A 的内容之一是更新输入 B。
shinyApp(
ui = fluidPage(
selectInput("A", "Input A", c(1:5)),
selectInput("B", "Input B", c(6:10))
),
server = function(input, output, session) {
observeEvent(input$A, ignoreInit = TRUE, {
message("Doing A stuff")
updateSelectInput(session, "B", selected = 10)
})
observeEvent(input$B, ignoreInit = TRUE, {
message("Doing B stuff")
})
}
)
Run Code Online (Sandbox Code Playgroud)
因此,更改输入 A 显然也会触发事件 B。我希望事件 B 仅在用户更改输入值时触发,而不是在 updateInput 完成更改时触发。有没有办法在计算表达式时暂停调度事件?我想要这样的东西:
shinyApp(
ui = fluidPage(
selectInput("A", "Input A", c(1:5)),
selectInput("B", "Input B", c(6:10))
),
server = function(input, output, session) {
observeEvent(input$A, ignoreInit = TRUE, {
message("Doing A stuff")
suspendEventScheduling()
updateSelectInput(session, "B", selected = 10)
resumeEventScheduling()
})
observeEvent(input$B, ignoreInit = TRUE, {
message("Doing B stuff")
})
}
)
Run Code Online (Sandbox Code Playgroud)
观察者文档提到“暂停状态”,但我找不到任何有关如何实际使用它的示例。
过去,我使用哨兵值模式来解决这些类型的情况(见下文)。但总感觉很脆弱。希望这个功能请求能带来更好的选择。
library(shiny)
shinyApp(
ui = fluidPage(
selectInput("A", "Input A", c(1:5)),
selectInput("B", "Input B", c(6:10))
),
server = function(input, output, session) {
is_server_update <- FALSE
observeEvent(input$A, {
message("Doing A stuff")
updateSelectInput(session, "B", selected = 10)
# Unchanged value doesn't trigger an invalidation
if (input$B != 10) {
is_server_update <<- TRUE
}
}, ignoreInit = TRUE)
observeEvent(input$B, {
if (is_server_update) {
is_server_update <<- FALSE
} else {
message("Doing B stuff")
}
}, ignoreInit = TRUE)
}
)
Run Code Online (Sandbox Code Playgroud)