| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/reactives.R |
| 3 |
\name{observe} |
| 4 |
\alias{observe} |
| 5 |
\title{Create a reactive observer} |
| 6 |
\usage{ |
| 7 |
observe( |
| 8 |
x, |
| 9 |
env = parent.frame(), |
| 10 |
quoted = FALSE, |
| 11 |
..., |
| 12 |
label = NULL, |
| 13 |
suspended = FALSE, |
| 14 |
priority = 0, |
| 15 |
domain = getDefaultReactiveDomain(), |
| 16 |
autoDestroy = TRUE, |
| 17 |
..stacktraceon = TRUE |
| 18 |
) |
| 19 |
} |
| 20 |
\arguments{ |
| 21 |
\item{x}{An expression (quoted or unquoted). Any return value will be |
| 22 |
ignored.} |
| 23 |
|
| 24 |
\item{env}{The parent environment for the reactive expression. By default, |
| 25 |
this is the calling environment, the same as when defining an ordinary |
| 26 |
non-reactive expression. If \code{x} is a quosure and \code{quoted} is \code{TRUE}, |
| 27 |
then \code{env} is ignored.} |
| 28 |
|
| 29 |
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{x} |
| 30 |
will be used when \code{x} is evaluated. If \code{x} is a quosure and you |
| 31 |
would like to use its expression as a value for \code{x}, then you must set |
| 32 |
\code{quoted} to \code{TRUE}.} |
| 33 |
|
| 34 |
\item{...}{Not used.} |
| 35 |
|
| 36 |
\item{label}{A label for the observer, useful for debugging.} |
| 37 |
|
| 38 |
\item{suspended}{If \code{TRUE}, start the observer in a suspended state. If |
| 39 |
\code{FALSE} (the default), start in a non-suspended state.} |
| 40 |
|
| 41 |
\item{priority}{An integer or numeric that controls the priority with which |
| 42 |
this observer should be executed. A higher value means higher priority: an |
| 43 |
observer with a higher priority value will execute before all observers |
| 44 |
with lower priority values. Positive, negative, and zero values are |
| 45 |
allowed.} |
| 46 |
|
| 47 |
\item{domain}{See \link{domains}.} |
| 48 |
|
| 49 |
\item{autoDestroy}{If \code{TRUE} (the default), the observer will be |
| 50 |
automatically destroyed when its domain (if any) ends.} |
| 51 |
|
| 52 |
\item{..stacktraceon}{Advanced use only. For stack manipulation purposes; see |
| 53 |
\code{\link[=stacktrace]{stacktrace()}}.} |
| 54 |
} |
| 55 |
\value{ |
| 56 |
An observer reference class object. This object has the following |
| 57 |
methods: |
| 58 |
\describe{ |
| 59 |
\item{\code{suspend()}}{ |
| 60 |
Causes this observer to stop scheduling flushes (re-executions) in |
| 61 |
response to invalidations. If the observer was invalidated prior to |
| 62 |
this call but it has not re-executed yet then that re-execution will |
| 63 |
still occur, because the flush is already scheduled. |
| 64 |
} |
| 65 |
\item{\code{resume()}}{ |
| 66 |
Causes this observer to start re-executing in response to |
| 67 |
invalidations. If the observer was invalidated while suspended, then it |
| 68 |
will schedule itself for re-execution. |
| 69 |
} |
| 70 |
\item{\code{destroy()}}{ |
| 71 |
Stops the observer from executing ever again, even if it is currently |
| 72 |
scheduled for re-execution. |
| 73 |
} |
| 74 |
\item{\code{setPriority(priority = 0)}}{ |
| 75 |
Change this observer's priority. Note that if the observer is currently |
| 76 |
invalidated, then the change in priority will not take effect until the |
| 77 |
next invalidation--unless the observer is also currently suspended, in |
| 78 |
which case the priority change will be effective upon resume. |
| 79 |
} |
| 80 |
\item{\code{setAutoDestroy(autoDestroy)}}{ |
| 81 |
Sets whether this observer should be automatically destroyed when its |
| 82 |
domain (if any) ends. If autoDestroy is TRUE and the domain already |
| 83 |
ended, then destroy() is called immediately." |
| 84 |
} |
| 85 |
\item{\code{onInvalidate(callback)}}{ |
| 86 |
Register a callback function to run when this observer is invalidated. |
| 87 |
No arguments will be provided to the callback function when it is |
| 88 |
invoked. |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
\description{ |
| 93 |
Creates an observer from the given expression. |
| 94 |
} |
| 95 |
\details{ |
| 96 |
An observer is like a reactive expression in that it can read reactive values |
| 97 |
and call reactive expressions, and will automatically re-execute when those |
| 98 |
dependencies change. But unlike reactive expressions, it doesn't yield a |
| 99 |
result and can't be used as an input to other reactive expressions. Thus, |
| 100 |
observers are only useful for their side effects (for example, performing |
| 101 |
I/O). |
| 102 |
|
| 103 |
Another contrast between reactive expressions and observers is their |
| 104 |
execution strategy. Reactive expressions use lazy evaluation; that is, when |
| 105 |
their dependencies change, they don't re-execute right away but rather wait |
| 106 |
until they are called by someone else. Indeed, if they are not called then |
| 107 |
they will never re-execute. In contrast, observers use eager evaluation; as |
| 108 |
soon as their dependencies change, they schedule themselves to re-execute. |
| 109 |
|
| 110 |
Starting with Shiny 0.10.0, observers are automatically destroyed by default |
| 111 |
when the \link[=domains]{domain} that owns them ends (e.g. when a Shiny |
| 112 |
session ends). |
| 113 |
} |
| 114 |
\examples{ |
| 115 |
values <- reactiveValues(A=1) |
| 116 |
|
| 117 |
obsB <- observe({ |
| 118 |
print(values$A + 1) |
| 119 |
}) |
| 120 |
|
| 121 |
# To store expressions for later conversion to observe, use rlang::quo() |
| 122 |
myquo <- rlang::quo({ print(values$A + 3) }) |
| 123 |
obsC <- rlang::inject(observe(!!myquo)) |
| 124 |
|
| 125 |
# (Legacy) Can use quoted expressions |
| 126 |
obsD <- observe(quote({ print(values$A + 2) }), quoted = TRUE) |
| 127 |
|
| 128 |
# In a normal Shiny app, the web client will trigger flush events. If you |
| 129 |
# are at the console, you can force a flush with flushReact() |
| 130 |
shiny:::flushReact() |
| 131 |
} |