| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/reactives.R |
| 3 |
\name{observeEvent} |
| 4 |
\alias{observeEvent} |
| 5 |
\alias{eventReactive} |
| 6 |
\title{Event handler} |
| 7 |
\usage{ |
| 8 |
observeEvent( |
| 9 |
eventExpr, |
| 10 |
handlerExpr, |
| 11 |
event.env = parent.frame(), |
| 12 |
event.quoted = FALSE, |
| 13 |
handler.env = parent.frame(), |
| 14 |
handler.quoted = FALSE, |
| 15 |
..., |
| 16 |
label = NULL, |
| 17 |
suspended = FALSE, |
| 18 |
priority = 0, |
| 19 |
domain = getDefaultReactiveDomain(), |
| 20 |
autoDestroy = TRUE, |
| 21 |
ignoreNULL = TRUE, |
| 22 |
ignoreInit = FALSE, |
| 23 |
once = FALSE |
| 24 |
) |
| 25 |
|
| 26 |
eventReactive( |
| 27 |
eventExpr, |
| 28 |
valueExpr, |
| 29 |
event.env = parent.frame(), |
| 30 |
event.quoted = FALSE, |
| 31 |
value.env = parent.frame(), |
| 32 |
value.quoted = FALSE, |
| 33 |
..., |
| 34 |
label = NULL, |
| 35 |
domain = getDefaultReactiveDomain(), |
| 36 |
ignoreNULL = TRUE, |
| 37 |
ignoreInit = FALSE |
| 38 |
) |
| 39 |
} |
| 40 |
\arguments{ |
| 41 |
\item{eventExpr}{A (quoted or unquoted) expression that represents the event; |
| 42 |
this can be a simple reactive value like \code{input$click}, a call to a |
| 43 |
reactive expression like \code{dataset()}, or even a complex expression |
| 44 |
inside curly braces} |
| 45 |
|
| 46 |
\item{handlerExpr}{The expression to call whenever \code{eventExpr} is |
| 47 |
invalidated. This should be a side-effect-producing action (the return |
| 48 |
value will be ignored). It will be executed within an \code{\link[=isolate]{isolate()}} |
| 49 |
scope.} |
| 50 |
|
| 51 |
\item{event.env}{The parent environment for the reactive expression. By default, |
| 52 |
this is the calling environment, the same as when defining an ordinary |
| 53 |
non-reactive expression. If \code{eventExpr} is a quosure and \code{event.quoted} is \code{TRUE}, |
| 54 |
then \code{event.env} is ignored.} |
| 55 |
|
| 56 |
\item{event.quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{eventExpr} |
| 57 |
will be used when \code{eventExpr} is evaluated. If \code{eventExpr} is a quosure and you |
| 58 |
would like to use its expression as a value for \code{eventExpr}, then you must set |
| 59 |
\code{event.quoted} to \code{TRUE}.} |
| 60 |
|
| 61 |
\item{handler.env}{The parent environment for the reactive expression. By default, |
| 62 |
this is the calling environment, the same as when defining an ordinary |
| 63 |
non-reactive expression. If \code{handlerExpr} is a quosure and \code{handler.quoted} is \code{TRUE}, |
| 64 |
then \code{handler.env} is ignored.} |
| 65 |
|
| 66 |
\item{handler.quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{handlerExpr} |
| 67 |
will be used when \code{handlerExpr} is evaluated. If \code{handlerExpr} is a quosure and you |
| 68 |
would like to use its expression as a value for \code{handlerExpr}, then you must set |
| 69 |
\code{handler.quoted} to \code{TRUE}.} |
| 70 |
|
| 71 |
\item{...}{Currently not used.} |
| 72 |
|
| 73 |
\item{label}{A label for the observer or reactive, useful for debugging.} |
| 74 |
|
| 75 |
\item{suspended}{If \code{TRUE}, start the observer in a suspended state. If |
| 76 |
\code{FALSE} (the default), start in a non-suspended state.} |
| 77 |
|
| 78 |
\item{priority}{An integer or numeric that controls the priority with which |
| 79 |
this observer should be executed. An observer with a given priority level |
| 80 |
will always execute sooner than all observers with a lower priority level. |
| 81 |
Positive, negative, and zero values are allowed.} |
| 82 |
|
| 83 |
\item{domain}{See \link{domains}.} |
| 84 |
|
| 85 |
\item{autoDestroy}{If \code{TRUE} (the default), the observer will be |
| 86 |
automatically destroyed when its domain (if any) ends.} |
| 87 |
|
| 88 |
\item{ignoreNULL}{Whether the action should be triggered (or value |
| 89 |
calculated, in the case of \code{eventReactive}) when the input event expression |
| 90 |
is \code{NULL}. See Details.} |
| 91 |
|
| 92 |
\item{ignoreInit}{If \code{TRUE}, then, when this \code{observeEvent} is |
| 93 |
first created/initialized, ignore the \code{handlerExpr} (the second |
| 94 |
argument), whether it is otherwise supposed to run or not. The default is |
| 95 |
\code{FALSE}. See Details.} |
| 96 |
|
| 97 |
\item{once}{Whether this \code{observeEvent} should be immediately destroyed |
| 98 |
after the first time that the code in \code{handlerExpr} is run. This |
| 99 |
pattern is useful when you want to subscribe to a event that should only |
| 100 |
happen once.} |
| 101 |
|
| 102 |
\item{valueExpr}{The expression that produces the return value of the |
| 103 |
\code{eventReactive}. It will be executed within an \code{\link[=isolate]{isolate()}} |
| 104 |
scope.} |
| 105 |
|
| 106 |
\item{value.env}{The parent environment for the reactive expression. By default, |
| 107 |
this is the calling environment, the same as when defining an ordinary |
| 108 |
non-reactive expression. If \code{valueExpr} is a quosure and \code{value.quoted} is \code{TRUE}, |
| 109 |
then \code{value.env} is ignored.} |
| 110 |
|
| 111 |
\item{value.quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{valueExpr} |
| 112 |
will be used when \code{valueExpr} is evaluated. If \code{valueExpr} is a quosure and you |
| 113 |
would like to use its expression as a value for \code{valueExpr}, then you must set |
| 114 |
\code{value.quoted} to \code{TRUE}.} |
| 115 |
} |
| 116 |
\value{ |
| 117 |
\code{observeEvent} returns an observer reference class object (see |
| 118 |
\code{\link[=observe]{observe()}}). \code{eventReactive} returns a reactive expression |
| 119 |
object (see \code{\link[=reactive]{reactive()}}). |
| 120 |
} |
| 121 |
\description{ |
| 122 |
Respond to "event-like" reactive inputs, values, and expressions. As of Shiny |
| 123 |
1.6.0, we recommend using \code{\link[=bindEvent]{bindEvent()}} instead of \code{eventReactive()} and |
| 124 |
\code{observeEvent()}. This is because \code{bindEvent()} can be composed with |
| 125 |
\code{\link[=bindCache]{bindCache()}}, and because it can also be used with \code{render} functions (like |
| 126 |
\code{\link[=renderText]{renderText()}} and \code{\link[=renderPlot]{renderPlot()}}). |
| 127 |
} |
| 128 |
\details{ |
| 129 |
Shiny's reactive programming framework is primarily designed for calculated |
| 130 |
values (reactive expressions) and side-effect-causing actions (observers) |
| 131 |
that respond to \emph{any} of their inputs changing. That's often what is |
| 132 |
desired in Shiny apps, but not always: sometimes you want to wait for a |
| 133 |
specific action to be taken from the user, like clicking an |
| 134 |
\code{\link[=actionButton]{actionButton()}}, before calculating an expression or taking an |
| 135 |
action. A reactive value or expression that is used to trigger other |
| 136 |
calculations in this way is called an \emph{event}. |
| 137 |
|
| 138 |
These situations demand a more imperative, "event handling" style of |
| 139 |
programming that is possible--but not particularly intuitive--using the |
| 140 |
reactive programming primitives \code{\link[=observe]{observe()}} and |
| 141 |
\code{\link[=isolate]{isolate()}}. \code{observeEvent} and \code{eventReactive} provide |
| 142 |
straightforward APIs for event handling that wrap \code{observe} and |
| 143 |
\code{isolate}. |
| 144 |
|
| 145 |
Use \code{observeEvent} whenever you want to \emph{perform an action} in |
| 146 |
response to an event. (Note that "recalculate a value" does not generally |
| 147 |
count as performing an action--see \code{eventReactive} for that.) The first |
| 148 |
argument is the event you want to respond to, and the second argument is a |
| 149 |
function that should be called whenever the event occurs. Note that |
| 150 |
\code{observeEvent()} is equivalent to using \code{observe() \%>\% bindEvent()} and as of |
| 151 |
Shiny 1.6.0, we recommend the latter. |
| 152 |
|
| 153 |
Use \code{eventReactive} to create a \emph{calculated value} that only |
| 154 |
updates in response to an event. This is just like a normal |
| 155 |
\link[=reactive]{reactive expression} except it ignores all the usual |
| 156 |
invalidations that come from its reactive dependencies; it only invalidates |
| 157 |
in response to the given event. Note that |
| 158 |
\code{eventReactive()} is equivalent to using \code{reactive() \%>\% bindEvent()} and as of |
| 159 |
Shiny 1.6.0, we recommend the latter. |
| 160 |
} |
| 161 |
\section{ignoreNULL and ignoreInit}{ |
| 162 |
|
| 163 |
|
| 164 |
Both \code{observeEvent} and \code{eventReactive} take an \code{ignoreNULL} |
| 165 |
parameter that affects behavior when the \code{eventExpr} evaluates to |
| 166 |
\code{NULL} (or in the special case of an \code{\link[=actionButton]{actionButton()}}, |
| 167 |
\code{0}). In these cases, if \code{ignoreNULL} is \code{TRUE}, then an |
| 168 |
\code{observeEvent} will not execute and an \code{eventReactive} will raise a |
| 169 |
silent \link[=validate]{validation} error. This is useful behavior if you |
| 170 |
don't want to do the action or calculation when your app first starts, but |
| 171 |
wait for the user to initiate the action first (like a "Submit" button); |
| 172 |
whereas \code{ignoreNULL=FALSE} is desirable if you want to initially perform |
| 173 |
the action/calculation and just let the user re-initiate it (like a |
| 174 |
"Recalculate" button). |
| 175 |
|
| 176 |
Likewise, both \code{observeEvent} and \code{eventReactive} also take in an |
| 177 |
\code{ignoreInit} argument. By default, both of these will run right when they |
| 178 |
are created (except if, at that moment, \code{eventExpr} evaluates to \code{NULL} |
| 179 |
and \code{ignoreNULL} is \code{TRUE}). But when responding to a click of an action |
| 180 |
button, it may often be useful to set \code{ignoreInit} to \code{TRUE}. For |
| 181 |
example, if you're setting up an \code{observeEvent} for a dynamically created |
| 182 |
button, then \code{ignoreInit = TRUE} will guarantee that the action (in |
| 183 |
\code{handlerExpr}) will only be triggered when the button is actually clicked, |
| 184 |
instead of also being triggered when it is created/initialized. Similarly, |
| 185 |
if you're setting up an \code{eventReactive} that responds to a dynamically |
| 186 |
created button used to refresh some data (then returned by that \code{eventReactive}), |
| 187 |
then you should use \verb{eventReactive([...], ignoreInit = TRUE)} if you want |
| 188 |
to let the user decide if/when they want to refresh the data (since, depending |
| 189 |
on the app, this may be a computationally expensive operation). |
| 190 |
|
| 191 |
Even though \code{ignoreNULL} and \code{ignoreInit} can be used for similar |
| 192 |
purposes they are independent from one another. Here's the result of combining |
| 193 |
these: |
| 194 |
|
| 195 |
\describe{ |
| 196 |
\item{\code{ignoreNULL = TRUE} and \code{ignoreInit = FALSE}}{ |
| 197 |
This is the default. This combination means that \code{handlerExpr}/ |
| 198 |
\code{valueExpr} will run every time that \code{eventExpr} is not |
| 199 |
\code{NULL}. If, at the time of the creation of the |
| 200 |
\code{observeEvent}/\code{eventReactive}, \code{eventExpr} happens |
| 201 |
to \emph{not} be \code{NULL}, then the code runs. |
| 202 |
} |
| 203 |
\item{\code{ignoreNULL = FALSE} and \code{ignoreInit = FALSE}}{ |
| 204 |
This combination means that \code{handlerExpr}/\code{valueExpr} will |
| 205 |
run every time no matter what. |
| 206 |
} |
| 207 |
\item{\code{ignoreNULL = FALSE} and \code{ignoreInit = TRUE}}{ |
| 208 |
This combination means that \code{handlerExpr}/\code{valueExpr} will |
| 209 |
\emph{not} run when the \code{observeEvent}/\code{eventReactive} is |
| 210 |
created (because \code{ignoreInit = TRUE}), but it will run every |
| 211 |
other time. |
| 212 |
} |
| 213 |
\item{\code{ignoreNULL = TRUE} and \code{ignoreInit = TRUE}}{ |
| 214 |
This combination means that \code{handlerExpr}/\code{valueExpr} will |
| 215 |
\emph{not} run when the \code{observeEvent}/\code{eventReactive} is |
| 216 |
created (because \code{ignoreInit = TRUE}). After that, |
| 217 |
\code{handlerExpr}/\code{valueExpr} will run every time that |
| 218 |
\code{eventExpr} is not \code{NULL}. |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
\examples{ |
| 224 |
## Only run examples in interactive R sessions |
| 225 |
if (interactive()) { |
| 226 |
|
| 227 |
## App 1: Sample usage |
| 228 |
shinyApp( |
| 229 |
ui = fluidPage( |
| 230 |
column(4, |
| 231 |
numericInput("x", "Value", 5), |
| 232 |
br(), |
| 233 |
actionButton("button", "Show") |
| 234 |
), |
| 235 |
column(8, tableOutput("table")) |
| 236 |
), |
| 237 |
server = function(input, output) { |
| 238 |
# Take an action every time button is pressed; |
| 239 |
# here, we just print a message to the console |
| 240 |
observeEvent(input$button, { |
| 241 |
cat("Showing", input$x, "rows\n") |
| 242 |
}) |
| 243 |
# The observeEvent() above is equivalent to: |
| 244 |
# observe({ |
| 245 |
# cat("Showing", input$x, "rows\n") |
| 246 |
# }) \%>\% |
| 247 |
# bindEvent(input$button) |
| 248 |
|
| 249 |
# Take a reactive dependency on input$button, but |
| 250 |
# not on any of the stuff inside the function |
| 251 |
df <- eventReactive(input$button, { |
| 252 |
head(cars, input$x) |
| 253 |
}) |
| 254 |
output$table <- renderTable({ |
| 255 |
df() |
| 256 |
}) |
| 257 |
} |
| 258 |
) |
| 259 |
|
| 260 |
## App 2: Using `once` |
| 261 |
shinyApp( |
| 262 |
ui = basicPage( actionButton("go", "Go")), |
| 263 |
server = function(input, output, session) { |
| 264 |
observeEvent(input$go, { |
| 265 |
print(paste("This will only be printed once; all", |
| 266 |
"subsequent button clicks won't do anything")) |
| 267 |
}, once = TRUE) |
| 268 |
# The observeEvent() above is equivalent to: |
| 269 |
# observe({ |
| 270 |
# print(paste("This will only be printed once; all", |
| 271 |
# "subsequent button clicks won't do anything")) |
| 272 |
# }) \%>\% |
| 273 |
# bindEvent(input$go, once = TRUE) |
| 274 |
} |
| 275 |
) |
| 276 |
|
| 277 |
## App 3: Using `ignoreInit` and `once` |
| 278 |
shinyApp( |
| 279 |
ui = basicPage(actionButton("go", "Go")), |
| 280 |
server = function(input, output, session) { |
| 281 |
observeEvent(input$go, { |
| 282 |
insertUI("#go", "afterEnd", |
| 283 |
actionButton("dynamic", "click to remove")) |
| 284 |
|
| 285 |
# set up an observer that depends on the dynamic |
| 286 |
# input, so that it doesn't run when the input is |
| 287 |
# created, and only runs once after that (since |
| 288 |
# the side effect is remove the input from the DOM) |
| 289 |
observeEvent(input$dynamic, { |
| 290 |
removeUI("#dynamic") |
| 291 |
}, ignoreInit = TRUE, once = TRUE) |
| 292 |
}) |
| 293 |
} |
| 294 |
) |
| 295 |
} |
| 296 |
} |
| 297 |
\seealso{ |
| 298 |
\code{\link[=actionButton]{actionButton()}} |
| 299 |
} |