r - 测试中的闪亮反应上下文
问题描述
我正在尝试执行需要反应上下文的代码(但不是整个服务器)
library(shiny)
library(testthat)
test_that("test ", {
withReactiveDomain(MockShinySession$new(), {
v <- reactiveVal()
v("abc")
val <- v()
expect_equal("abc", val)
})
})
然后我收到以下错误:
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Backtrace:
1. shiny::withReactiveDomain(...) test_integration.R:45:2
5. shiny:::v() test_integration.R:48:4
6. rv$get()
7. private$dependents$register()
8. shiny:::getCurrentContext()
9. .getReactiveEnvironment()$currentContext()
我错过了什么?如果我在 R 会话中执行内部块,我会得到同样的错误。
解决方案
具有未导出功能的解决方案flushReact
:
library(shiny)
x <- reactiveVal()
observe({
message(x())
})
x("abc")
capture.output(shiny:::flushReact(), type = "message")
# [1] "abc"
推荐阅读
- firebase - Firebase Admin 6.2.0 升级问题:服务器无法访问实时数据库
- apache-spark - 获取被筛选器从 spark 数据框中删除的行的示例
- api - Prometheus 为 .NET core api 返回错误“服务器返回 HTTP 状态 401 未授权”
- vba - 使用宏按钮根据模板和主工作表上的列表添加新选项卡
- python - 在python中发送telnet命令
- c# - Classlibrary 输出类型中的 Npgsql 程序集错误
- javascript - 过滤firebase数据库中的数据时遇到问题
- django - 在 Elastic Beanstalk 上启动 SQS celery worker
- c# - 如何禁用打开 WPF 弹出窗口
- amazon-web-services - Amazon lex 匹配完全不相似的 Intent