我在测试
shiny
模块时遇到以下问题,其中观察者依赖于无功值的变化。
srv_module <- function(id, n) {
moduleServer(id, function(input, output, session) {
data <- eventReactive(n(), ignoreNULL = TRUE, {
runif(n())
})
observeEvent(data(), ignoreNULL = TRUE, {
message("hi")
})
})
}
以下测试应该返回
data
的值,该值可以工作,但也应该打印来自观察者的消息,但不会发生。
testServer(
srv_module,
args = list(id = "test", n = reactiveVal(1)),
expr = {
print(data())
}
)
有人知道如何触发
observeEvent
吗?
谢谢!
要触发
testServer
中的观察者,必须调用 session$flushReact()
。观察者不会自动触发(即使在初始化时),并且每次都需要手动触发。
library(shiny)
srv_module <- function(id, n) {
moduleServer(id, function(input, output, session) {
data <- eventReactive(n(), ignoreNULL = TRUE, {
runif(n())
})
observeEvent(data(), {
message("hi1")
})
observeEvent(data(), {
message("hi2")
})
})
}
testServer(
srv_module,
args = list(id = "test", n = reactiveVal(1)),
expr = {
session$flushReact()
}
)
# hi1
# hi2