Debian Codegraph @

R/test-export.R

raw
1 #' Register expressions for export in test mode
2 #'
3 #' This function registers expressions that will be evaluated when a test export
4 #' event occurs. These events are triggered by accessing a snapshot URL.
5 #'
6 #' This function only has an effect if the app is launched in test mode. This is
7 #' done by calling `runApp()` with `test.mode=TRUE`, or by setting the
8 #' global option `shiny.testmode` to `TRUE`.
9 #'
10 #' @param quoted_ Are the expression quoted? Default is `FALSE`.
11 #' @param env_ The environment in which the expression should be evaluated.
12 #' @param session_ A Shiny session object.
13 #' @param ... Named arguments that are quoted or unquoted expressions that will
14 #' be captured and evaluated when snapshot URL is visited.
15 #' @examples
16 #' ## Only run this example in interactive R sessions
17 #' if (interactive()) {
18 #'
19 #' options(shiny.testmode = TRUE)
20 #'
21 #' # This application shows the test snapshot URL; clicking on it will
22 #' # fetch the input, output, and exported values in JSON format.
23 #' shinyApp(
24 #' ui = basicPage(
25 #' h4("Snapshot URL: "),
26 #' uiOutput("url"),
27 #' h4("Current values:"),
28 #' verbatimTextOutput("values"),
29 #' actionButton("inc", "Increment x")
30 #' ),
31 #'
32 #' server = function(input, output, session) {
33 #' vals <- reactiveValues(x = 1)
34 #' y <- reactive({ vals$x + 1 })
35 #'
36 #' observeEvent(input$inc, {
37 #' vals$x <<- vals$x + 1
38 #' })
39 #'
40 #' exportTestValues(
41 #' x = vals$x,
42 #' y = y()
43 #' )
44 #'
45 #' output$url <- renderUI({
46 #' url <- session$getTestSnapshotUrl(format="json")
47 #' a(href = url, url)
48 #' })
49 #'
50 #' output$values <- renderText({
51 #' paste0("vals$x: ", vals$x, "\ny: ", y())
52 #' })
53 #' }
54 #' )
55 #' }
56 #' @export
57 exportTestValues <- function(..., quoted_ = FALSE, env_ = parent.frame(),
58 session_ = getDefaultReactiveDomain())
59 {
60 session_$exportTestValues(..., quoted_ = quoted_, env_ = env_)
61 }