Debian Codegraph @

man/bindEvent.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/bind-event.R
3 \name{bindEvent}
4 \alias{bindEvent}
5 \title{Make an object respond only to specified reactive events}
6 \usage{
7 bindEvent(
8 x,
9 ...,
10 ignoreNULL = TRUE,
11 ignoreInit = FALSE,
12 once = FALSE,
13 label = NULL
14 )
15 }
16 \arguments{
17 \item{x}{An object to wrap so that is triggered only when a the specified
18 event occurs.}
19
20 \item{...}{One or more expressions that represents the event; this can be a
21 simple reactive value like \code{input$click}, a call to a reactive expression
22 like \code{dataset()}, or even a complex expression inside curly braces. If
23 there are multiple expressions in the \code{...}, then it will take a dependency
24 on all of them.}
25
26 \item{ignoreNULL}{Whether the action should be triggered (or value
27 calculated) when the input is \code{NULL}. See Details.}
28
29 \item{ignoreInit}{If \code{TRUE}, then, when the eventified object is first
30 created/initialized, don't trigger the action or (compute the value). The
31 default is \code{FALSE}. See Details.}
32
33 \item{once}{Used only for observers. Whether this \code{observer} should be
34 immediately destroyed after the first time that the code in the observer is
35 run. This pattern is useful when you want to subscribe to a event that
36 should only happen once.}
37
38 \item{label}{A label for the observer or reactive, useful for debugging.}
39 }
40 \description{
41 Modify an object to respond to "event-like" reactive inputs, values, and
42 expressions. \code{bindEvent()} can be used with reactive expressions, render
43 functions, and observers. The resulting object takes a reactive dependency on
44 the \code{...} arguments, and not on the original object's code. This can, for
45 example, be used to make an observer execute only when a button is pressed.
46
47 \code{bindEvent()} was added in Shiny 1.6.0. When it is used with \code{\link[=reactive]{reactive()}} and
48 \code{\link[=observe]{observe()}}, it does the same thing as \code{\link[=eventReactive]{eventReactive()}} and
49 \code{\link[=observeEvent]{observeEvent()}}. However, \code{bindEvent()} is more flexible: it can be combined
50 with \code{\link[=bindCache]{bindCache()}}, and it can also be used with \code{render} functions (like
51 \code{\link[=renderText]{renderText()}} and \code{\link[=renderPlot]{renderPlot()}}).
52 }
53 \section{Details}{
54
55
56 Shiny's reactive programming framework is primarily designed for calculated
57 values (reactive expressions) and side-effect-causing actions (observers)
58 that respond to \emph{any} of their inputs changing. That's often what is
59 desired in Shiny apps, but not always: sometimes you want to wait for a
60 specific action to be taken from the user, like clicking an
61 \code{\link[=actionButton]{actionButton()}}, before calculating an expression or taking an action. A
62 reactive value or expression that is used to trigger other calculations in
63 this way is called an \emph{event}.
64
65 These situations demand a more imperative, "event handling" style of
66 programming that is possible--but not particularly intuitive--using the
67 reactive programming primitives \code{\link[=observe]{observe()}} and \code{\link[=isolate]{isolate()}}. \code{bindEvent()}
68 provides a straightforward API for event handling that wraps \code{observe} and
69 \code{isolate}.
70
71 The \code{...} arguments are captured as expressions and combined into an
72 \strong{event expression}. When this event expression is invalidated (when its
73 upstream reactive inputs change), that is an \strong{event}, and it will cause
74 the original object's code to execute.
75
76 Use \code{bindEvent()} with \code{observe()} whenever you want to \emph{perform an action}
77 in response to an event. (This does the same thing as \code{\link[=observeEvent]{observeEvent()}},
78 which was available in Shiny prior to version 1.6.0.) Note that
79 "recalculate a value" does not generally count as performing an action --
80 use \code{\link[=reactive]{reactive()}} for that.
81
82 Use \code{bindEvent()} with \code{reactive()} to create a \emph{calculated value} that
83 only updates in response to an event. This is just like a normal \link[=reactive]{reactive expression} except it ignores all the usual invalidations that
84 come from its reactive dependencies; it only invalidates in response to the
85 given event. (This does the same thing as \code{\link[=eventReactive]{eventReactive()}}, which was
86 available in Shiny prior to version 1.6.0.)
87
88 \code{bindEvent()} is often used with \code{\link[=bindCache]{bindCache()}}.
89 }
90
91 \section{ignoreNULL and ignoreInit}{
92
93
94 \code{bindEvent()} takes an \code{ignoreNULL} parameter that affects behavior when
95 the event expression evaluates to \code{NULL} (or in the special case of an
96 \code{\link[=actionButton]{actionButton()}}, \code{0}). In these cases, if \code{ignoreNULL} is \code{TRUE}, then it
97 will raise a silent \link[=validate]{validation} error. This is useful behavior
98 if you don't want to do the action or calculation when your app first
99 starts, but wait for the user to initiate the action first (like a "Submit"
100 button); whereas \code{ignoreNULL=FALSE} is desirable if you want to initially
101 perform the action/calculation and just let the user re-initiate it (like a
102 "Recalculate" button).
103
104 \code{bindEvent()} also takes an \code{ignoreInit} argument. By default, reactive
105 expressions and observers will run on the first reactive flush after they
106 are created (except if, at that moment, the event expression evaluates to
107 \code{NULL} and \code{ignoreNULL} is \code{TRUE}). But when responding to a click of an
108 action button, it may often be useful to set \code{ignoreInit} to \code{TRUE}. For
109 example, if you're setting up an observer to respond to a dynamically
110 created button, then \code{ignoreInit = TRUE} will guarantee that the action
111 will only be triggered when the button is actually clicked, instead of also
112 being triggered when it is created/initialized. Similarly, if you're
113 setting up a reactive that responds to a dynamically created button used to
114 refresh some data (which is then returned by that \code{reactive}), then you
115 should use \code{reactive(...) \%>\% bindEvent(..., ignoreInit = TRUE)} if you
116 want to let the user decide if/when they want to refresh the data (since,
117 depending on the app, this may be a computationally expensive operation).
118
119 Even though \code{ignoreNULL} and \code{ignoreInit} can be used for similar purposes
120 they are independent from one another. Here's the result of combining
121 these:
122
123 \describe{
124 \item{\code{ignoreNULL = TRUE} and \code{ignoreInit = FALSE}}{
125 This is the default. This combination means that reactive/observer code
126 will run every time that event expression is not
127 \code{NULL}. If, at the time of creation, the event expression happens
128 to \emph{not} be \code{NULL}, then the code runs.
129 }
130 \item{\code{ignoreNULL = FALSE} and \code{ignoreInit = FALSE}}{
131 This combination means that reactive/observer code will
132 run every time no matter what.
133 }
134 \item{\code{ignoreNULL = FALSE} and \code{ignoreInit = TRUE}}{
135 This combination means that reactive/observer code will
136 \emph{not} run at the time of creation (because \code{ignoreInit = TRUE}),
137 but it will run every other time.
138 }
139 \item{\code{ignoreNULL = TRUE} and \code{ignoreInit = TRUE}}{
140 This combination means that reactive/observer code will
141 \emph{not} at the time of creation (because \code{ignoreInit = TRUE}).
142 After that, the reactive/observer code will run every time that
143 the event expression is not \code{NULL}.
144 }
145 }
146 }
147
148 \section{Types of objects}{
149
150
151 \code{bindEvent()} can be used with reactive expressions, observers, and shiny
152 render functions.
153
154 When \code{bindEvent()} is used with \code{reactive()}, it creates a new reactive
155 expression object.
156
157 When \code{bindEvent()} is used with \code{observe()}, it alters the observer in
158 place. It can only be used with observers which have not yet executed.
159 }
160
161 \section{Combining events and caching}{
162
163
164 In many cases, it makes sense to use \code{bindEvent()} along with
165 \code{bindCache()}, because they each can reduce the amount of work done on the
166 server. For example, you could have \link{sliderInput}s \code{x} and \code{y} and a
167 \code{reactive()} that performs a time-consuming operation with those values.
168 Using \code{bindCache()} can speed things up, especially if there are multiple
169 users. But it might make sense to also not do the computation until the
170 user sets both \code{x} and \code{y}, and then clicks on an \link{actionButton} named
171 \code{go}.
172
173 To use both caching and events, the object should first be passed to
174 \code{bindCache()}, then \code{bindEvent()}. For example:
175
176 \if{html}{\out{<div class="sourceCode">}}\preformatted{r <- reactive(\{
177 Sys.sleep(2) # Pretend this is an expensive computation
178 input$x * input$y
179 \}) \%>\%
180 bindCache(input$x, input$y) \%>\%
181 bindEvent(input$go)
182 }\if{html}{\out{</div>}}
183
184 Anything that consumes \code{r()} will take a reactive dependency on the event
185 expression given to \code{bindEvent()}, and not the cache key expression given to
186 \code{bindCache()}. In this case, it is just \code{input$go}.
187 }
188