Debian Codegraph @

man/isolate.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/reactives.R
3 \name{isolate}
4 \alias{isolate}
5 \title{Create a non-reactive scope for an expression}
6 \usage{
7 isolate(expr)
8 }
9 \arguments{
10 \item{expr}{An expression that can access reactive values or expressions.}
11 }
12 \description{
13 Executes the given expression in a scope where reactive values or expression
14 can be read, but they cannot cause the reactive scope of the caller to be
15 re-evaluated when they change.
16 }
17 \details{
18 Ordinarily, the simple act of reading a reactive value causes a relationship
19 to be established between the caller and the reactive value, where a change
20 to the reactive value will cause the caller to re-execute. (The same applies
21 for the act of getting a reactive expression's value.) The \code{isolate}
22 function lets you read a reactive value or expression without establishing this
23 relationship.
24
25 The expression given to \code{isolate()} is evaluated in the calling
26 environment. This means that if you assign a variable inside the
27 \code{isolate()}, its value will be visible outside of the \code{isolate()}.
28 If you want to avoid this, you can use \code{\link[base:eval]{base::local()}} inside the
29 \code{isolate()}.
30
31 This function can also be useful for calling reactive expression at the
32 console, which can be useful for debugging. To do so, simply wrap the
33 calls to the reactive expression with \code{isolate()}.
34 }
35 \examples{
36 \dontrun{
37 observe({
38 input$saveButton # Do take a dependency on input$saveButton
39
40 # isolate a simple expression
41 data <- get(isolate(input$dataset)) # No dependency on input$dataset
42 writeToDatabase(data)
43 })
44
45 observe({
46 input$saveButton # Do take a dependency on input$saveButton
47
48 # isolate a whole block
49 data <- isolate({
50 a <- input$valueA # No dependency on input$valueA or input$valueB
51 b <- input$valueB
52 c(a=a, b=b)
53 })
54 writeToDatabase(data)
55 })
56
57 observe({
58 x <- 1
59 # x outside of isolate() is affected
60 isolate(x <- 2)
61 print(x) # 2
62
63 y <- 1
64 # Use local() to avoid affecting calling environment
65 isolate(local(y <- 2))
66 print(y) # 1
67 })
68
69 }
70
71 # Can also use isolate to call reactive expressions from the R console
72 values <- reactiveValues(A=1)
73 fun <- reactive({ as.character(values$A) })
74 isolate(fun())
75 # "1"
76
77 # isolate also works if the reactive expression accesses values from the
78 # input object, like input$x
79 }