Debian Codegraph @

man/reactive.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/reactives.R
3 \name{reactive}
4 \alias{reactive}
5 \alias{is.reactive}
6 \title{Create a reactive expression}
7 \usage{
8 reactive(
9 x,
10 env = parent.frame(),
11 quoted = FALSE,
12 ...,
13 label = NULL,
14 domain = getDefaultReactiveDomain(),
15 ..stacktraceon = TRUE
16 )
17
18 is.reactive(x)
19 }
20 \arguments{
21 \item{x}{For \code{is.reactive()}, an object to test. For \code{reactive()}, an
22 expression. When passing in a \code{\link[rlang:defusing-advanced]{rlang::quo()}}sure with \code{reactive()},
23 remember to use \code{\link[rlang:inject]{rlang::inject()}} to distinguish that you are passing in
24 the content of your quosure, not the expression of the quosure.}
25
26 \item{env}{The parent environment for the reactive expression. By default,
27 this is the calling environment, the same as when defining an ordinary
28 non-reactive expression. If \code{x} is a quosure and \code{quoted} is \code{TRUE},
29 then \code{env} is ignored.}
30
31 \item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{x}
32 will be used when \code{x} is evaluated. If \code{x} is a quosure and you
33 would like to use its expression as a value for \code{x}, then you must set
34 \code{quoted} to \code{TRUE}.}
35
36 \item{...}{Not used.}
37
38 \item{label}{A label for the reactive expression, useful for debugging.}
39
40 \item{domain}{See \link{domains}.}
41
42 \item{..stacktraceon}{Advanced use only. For stack manipulation purposes; see
43 \code{\link[=stacktrace]{stacktrace()}}.}
44 }
45 \value{
46 a function, wrapped in a S3 class "reactive"
47 }
48 \description{
49 Wraps a normal expression to create a reactive expression. Conceptually, a
50 reactive expression is a expression whose result will change over time.
51 }
52 \details{
53 Reactive expressions are expressions that can read reactive values and call
54 other reactive expressions. Whenever a reactive value changes, any reactive
55 expressions that depended on it are marked as "invalidated" and will
56 automatically re-execute if necessary. If a reactive expression is marked as
57 invalidated, any other reactive expressions that recently called it are also
58 marked as invalidated. In this way, invalidations ripple through the
59 expressions that depend on each other.
60
61 See the \href{https://shiny.rstudio.com/tutorial/}{Shiny tutorial} for
62 more information about reactive expressions.
63 }
64 \examples{
65 library(rlang)
66 values <- reactiveValues(A=1)
67
68 reactiveB <- reactive({
69 values$A + 1
70 })
71 # View the values from the R console with isolate()
72 isolate(reactiveB())
73 # 2
74
75 # To store expressions for later conversion to reactive, use quote()
76 myquo <- rlang::quo(values$A + 2)
77 # Unexpected value! Sending a quosure directly will not work as expected.
78 reactiveC <- reactive(myquo)
79 # We'd hope for `3`, but instead we get the quosure that was supplied.
80 isolate(reactiveC())
81
82 # Instead, the quosure should be `rlang::inject()`ed
83 reactiveD <- rlang::inject(reactive(!!myquo))
84 isolate(reactiveD())
85 # 3
86
87 # (Legacy) Can use quoted expressions
88 expr <- quote({ values$A + 3 })
89 reactiveE <- reactive(expr, quoted = TRUE)
90 isolate(reactiveE())
91 # 4
92
93 }