I am trying to execute code that requires a reactive context (but not the whole server)
library(shiny)
library(testthat)
test_that("test ", {
withReactiveDomain(MockShinySession$new(), {
v <- reactiveVal()
v("abc")
val <- v()
expect_equal("abc", val)
})
})
Then I get the following error:
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()
What am I missing? If I execute the inner block in an R session I get the same error.
A solution with the unexported function flushReact
:
library(shiny)
x <- reactiveVal()
observe({
message(x())
})
x("abc")
capture.output(shiny:::flushReact(), type = "message")
# [1] "abc"
Solution by @anddt
library(shiny)
library(testthat)
test_that("test ", {
withReactiveDomain(MockShinySession$new(), {
v <- reactiveVal()
v("abc")
val <- isolate(v())
expect_equal("abc", val)
})
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.