Files
shiny/reference/observeEvent.html
2025-12-09 21:32:58 +00:00

413 lines
32 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Event handler — observeEvent • shiny</title><!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous"><script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css"><script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous"><!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet"><script src="../pkgdown.js"></script><meta property="og:title" content="Event handler — observeEvent"><meta property="og:description" content='Respond to "event-like" reactive inputs, values, and expressions. As of Shiny
1.6.0, we recommend using bindEvent() instead of eventReactive() and
observeEvent(). This is because bindEvent() can be composed with
bindCache(), and because it can also be used with render functions (like
renderText() and renderPlot()).'><meta property="og:image" content="/logo.png"><!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--></head><body data-spy="scroll" data-target="#toc">
<div class="container template-reference-topic">
<header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">shiny</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">1.12.1</span>
</span>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav"><li>
<a href="../reference/index.html">Reference</a>
</li>
<li>
<a href="../news/index.html">Changelog</a>
</li>
</ul><ul class="nav navbar-nav navbar-right"><li>
<a href="https://github.com/rstudio/shiny/" class="external-link">
<span class="fab fa-github fa-lg"></span>
</a>
</li>
</ul></div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
</header><div class="row">
<div class="col-md-9 contents">
<div class="page-header">
<h1>Event handler</h1>
<small class="dont-index">Source: <a href="https://github.com/rstudio/shiny/blob/rc-v1.12.1/R/reactives.R" class="external-link"><code>R/reactives.R</code></a></small>
<div class="hidden name"><code>observeEvent.Rd</code></div>
</div>
<div class="ref-description">
<p>Respond to "event-like" reactive inputs, values, and expressions. As of Shiny
1.6.0, we recommend using <code><a href="bindEvent.html">bindEvent()</a></code> instead of <code>eventReactive()</code> and
<code>observeEvent()</code>. This is because <code><a href="bindEvent.html">bindEvent()</a></code> can be composed with
<code><a href="bindCache.html">bindCache()</a></code>, and because it can also be used with <code>render</code> functions (like
<code><a href="renderPrint.html">renderText()</a></code> and <code><a href="renderPlot.html">renderPlot()</a></code>).</p>
</div>
<div id="ref-usage">
<div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">observeEvent</span><span class="op">(</span></span>
<span> <span class="va">eventExpr</span>,</span>
<span> <span class="va">handlerExpr</span>,</span>
<span> event.env <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/sys.parent.html" class="external-link">parent.frame</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span> event.quoted <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> handler.env <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/sys.parent.html" class="external-link">parent.frame</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span> handler.quoted <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> <span class="va">...</span>,</span>
<span> label <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span> suspended <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> priority <span class="op">=</span> <span class="fl">0</span>,</span>
<span> domain <span class="op">=</span> <span class="fu"><a href="domains.html">getDefaultReactiveDomain</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span> autoDestroy <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> ignoreNULL <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> ignoreInit <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> once <span class="op">=</span> <span class="cn">FALSE</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="fu">eventReactive</span><span class="op">(</span></span>
<span> <span class="va">eventExpr</span>,</span>
<span> <span class="va">valueExpr</span>,</span>
<span> event.env <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/sys.parent.html" class="external-link">parent.frame</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span> event.quoted <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> value.env <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/sys.parent.html" class="external-link">parent.frame</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span> value.quoted <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> <span class="va">...</span>,</span>
<span> label <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span> domain <span class="op">=</span> <span class="fu"><a href="domains.html">getDefaultReactiveDomain</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span> ignoreNULL <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> ignoreInit <span class="op">=</span> <span class="cn">FALSE</span></span>
<span><span class="op">)</span></span></code></pre></div>
</div>
<div id="arguments">
<h2>Arguments</h2>
<dl><dt id="arg-eventexpr">eventExpr<a class="anchor" aria-label="anchor" href="#arg-eventexpr"></a></dt>
<dd><p>A (quoted or unquoted) expression that represents the event;
this can be a simple reactive value like <code>input$click</code>, a call to a
reactive expression like <code>dataset()</code>, or even a complex expression
inside curly braces</p></dd>
<dt id="arg-handlerexpr">handlerExpr<a class="anchor" aria-label="anchor" href="#arg-handlerexpr"></a></dt>
<dd><p>The expression to call whenever <code>eventExpr</code> is
invalidated. This should be a side-effect-producing action (the return
value will be ignored). It will be executed within an <code><a href="isolate.html">isolate()</a></code>
scope.</p></dd>
<dt id="arg-event-env">event.env<a class="anchor" aria-label="anchor" href="#arg-event-env"></a></dt>
<dd><p>The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If <code>eventExpr</code> is a quosure and <code>event.quoted</code> is <code>TRUE</code>,
then <code>event.env</code> is ignored.</p></dd>
<dt id="arg-event-quoted">event.quoted<a class="anchor" aria-label="anchor" href="#arg-event-quoted"></a></dt>
<dd><p>If it is <code>TRUE</code>, then the <code><a href="https://rdrr.io/r/base/substitute.html" class="external-link">quote()</a></code>ed value of <code>eventExpr</code>
will be used when <code>eventExpr</code> is evaluated. If <code>eventExpr</code> is a quosure and you
would like to use its expression as a value for <code>eventExpr</code>, then you must set
<code>event.quoted</code> to <code>TRUE</code>.</p></dd>
<dt id="arg-handler-env">handler.env<a class="anchor" aria-label="anchor" href="#arg-handler-env"></a></dt>
<dd><p>The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If <code>handlerExpr</code> is a quosure and <code>handler.quoted</code> is <code>TRUE</code>,
then <code>handler.env</code> is ignored.</p></dd>
<dt id="arg-handler-quoted">handler.quoted<a class="anchor" aria-label="anchor" href="#arg-handler-quoted"></a></dt>
<dd><p>If it is <code>TRUE</code>, then the <code><a href="https://rdrr.io/r/base/substitute.html" class="external-link">quote()</a></code>ed value of <code>handlerExpr</code>
will be used when <code>handlerExpr</code> is evaluated. If <code>handlerExpr</code> is a quosure and you
would like to use its expression as a value for <code>handlerExpr</code>, then you must set
<code>handler.quoted</code> to <code>TRUE</code>.</p></dd>
<dt id="arg--">...<a class="anchor" aria-label="anchor" href="#arg--"></a></dt>
<dd><p>Currently not used.</p></dd>
<dt id="arg-label">label<a class="anchor" aria-label="anchor" href="#arg-label"></a></dt>
<dd><p>A label for the observer or reactive, useful for debugging.</p></dd>
<dt id="arg-suspended">suspended<a class="anchor" aria-label="anchor" href="#arg-suspended"></a></dt>
<dd><p>If <code>TRUE</code>, start the observer in a suspended state. If
<code>FALSE</code> (the default), start in a non-suspended state.</p></dd>
<dt id="arg-priority">priority<a class="anchor" aria-label="anchor" href="#arg-priority"></a></dt>
<dd><p>An integer or numeric that controls the priority with which
this observer should be executed. An observer with a given priority level
will always execute sooner than all observers with a lower priority level.
Positive, negative, and zero values are allowed.</p></dd>
<dt id="arg-domain">domain<a class="anchor" aria-label="anchor" href="#arg-domain"></a></dt>
<dd><p>See <a href="domains.html">domains</a>.</p></dd>
<dt id="arg-autodestroy">autoDestroy<a class="anchor" aria-label="anchor" href="#arg-autodestroy"></a></dt>
<dd><p>If <code>TRUE</code> (the default), the observer will be
automatically destroyed when its domain (if any) ends.</p></dd>
<dt id="arg-ignorenull">ignoreNULL<a class="anchor" aria-label="anchor" href="#arg-ignorenull"></a></dt>
<dd><p>Whether the action should be triggered (or value
calculated, in the case of <code>eventReactive</code>) when the input event expression
is <code>NULL</code>. See Details.</p></dd>
<dt id="arg-ignoreinit">ignoreInit<a class="anchor" aria-label="anchor" href="#arg-ignoreinit"></a></dt>
<dd><p>If <code>TRUE</code>, then, when this <code>observeEvent</code> is
first created/initialized, ignore the <code>handlerExpr</code> (the second
argument), whether it is otherwise supposed to run or not. The default is
<code>FALSE</code>. See Details.</p></dd>
<dt id="arg-once">once<a class="anchor" aria-label="anchor" href="#arg-once"></a></dt>
<dd><p>Whether this <code>observeEvent</code> should be immediately destroyed
after the first time that the code in <code>handlerExpr</code> is run. This
pattern is useful when you want to subscribe to a event that should only
happen once.</p></dd>
<dt id="arg-valueexpr">valueExpr<a class="anchor" aria-label="anchor" href="#arg-valueexpr"></a></dt>
<dd><p>The expression that produces the return value of the
<code>eventReactive</code>. It will be executed within an <code><a href="isolate.html">isolate()</a></code>
scope.</p></dd>
<dt id="arg-value-env">value.env<a class="anchor" aria-label="anchor" href="#arg-value-env"></a></dt>
<dd><p>The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If <code>valueExpr</code> is a quosure and <code>value.quoted</code> is <code>TRUE</code>,
then <code>value.env</code> is ignored.</p></dd>
<dt id="arg-value-quoted">value.quoted<a class="anchor" aria-label="anchor" href="#arg-value-quoted"></a></dt>
<dd><p>If it is <code>TRUE</code>, then the <code><a href="https://rdrr.io/r/base/substitute.html" class="external-link">quote()</a></code>ed value of <code>valueExpr</code>
will be used when <code>valueExpr</code> is evaluated. If <code>valueExpr</code> is a quosure and you
would like to use its expression as a value for <code>valueExpr</code>, then you must set
<code>value.quoted</code> to <code>TRUE</code>.</p></dd>
</dl></div>
<div id="value">
<h2>Value</h2>
<p><code>observeEvent</code> returns an observer reference class object (see
<code><a href="observe.html">observe()</a></code>). <code>eventReactive</code> returns a reactive expression
object (see <code><a href="reactive.html">reactive()</a></code>).</p>
</div>
<div id="details">
<h2>Details</h2>
<p>Shiny's reactive programming framework is primarily designed for calculated
values (reactive expressions) and side-effect-causing actions (observers)
that respond to <em>any</em> of their inputs changing. That's often what is
desired in Shiny apps, but not always: sometimes you want to wait for a
specific action to be taken from the user, like clicking an
<code><a href="actionButton.html">actionButton()</a></code>, before calculating an expression or taking an
action. A reactive value or expression that is used to trigger other
calculations in this way is called an <em>event</em>.</p>
<p>These situations demand a more imperative, "event handling" style of
programming that is possiblebut not particularly intuitiveusing the
reactive programming primitives <code><a href="observe.html">observe()</a></code> and
<code><a href="isolate.html">isolate()</a></code>. <code>observeEvent</code> and <code>eventReactive</code> provide
straightforward APIs for event handling that wrap <code>observe</code> and
<code>isolate</code>.</p>
<p>Use <code>observeEvent</code> whenever you want to <em>perform an action</em> in
response to an event. (Note that "recalculate a value" does not generally
count as performing an actionsee <code>eventReactive</code> for that.) The first
argument is the event you want to respond to, and the second argument is a
function that should be called whenever the event occurs. Note that
<code>observeEvent()</code> is equivalent to using <code>observe() %&gt;% bindEvent()</code> and as of
Shiny 1.6.0, we recommend the latter.</p>
<p>Use <code>eventReactive</code> to create a <em>calculated value</em> that only
updates in response to an event. This is just like a normal
<a href="reactive.html">reactive expression</a> except it ignores all the usual
invalidations that come from its reactive dependencies; it only invalidates
in response to the given event. Note that
<code>eventReactive()</code> is equivalent to using <code>reactive() %&gt;% bindEvent()</code> and as of
Shiny 1.6.0, we recommend the latter.</p>
</div>
<div id="ignorenull-and-ignoreinit">
<h2>ignoreNULL and ignoreInit</h2>
<p>Both <code>observeEvent</code> and <code>eventReactive</code> take an <code>ignoreNULL</code>
parameter that affects behavior when the <code>eventExpr</code> evaluates to
<code>NULL</code> (or in the special case of an <code><a href="actionButton.html">actionButton()</a></code>,
<code>0</code>). In these cases, if <code>ignoreNULL</code> is <code>TRUE</code>, then an
<code>observeEvent</code> will not execute and an <code>eventReactive</code> will raise a
silent <a href="validate.html">validation</a> error. This is useful behavior if you
don't want to do the action or calculation when your app first starts, but
wait for the user to initiate the action first (like a "Submit" button);
whereas <code>ignoreNULL=FALSE</code> is desirable if you want to initially perform
the action/calculation and just let the user re-initiate it (like a
"Recalculate" button).</p>
<p>Likewise, both <code>observeEvent</code> and <code>eventReactive</code> also take in an
<code>ignoreInit</code> argument. By default, both of these will run right when they
are created (except if, at that moment, <code>eventExpr</code> evaluates to <code>NULL</code>
and <code>ignoreNULL</code> is <code>TRUE</code>). But when responding to a click of an action
button, it may often be useful to set <code>ignoreInit</code> to <code>TRUE</code>. For
example, if you're setting up an <code>observeEvent</code> for a dynamically created
button, then <code>ignoreInit = TRUE</code> will guarantee that the action (in
<code>handlerExpr</code>) will only be triggered when the button is actually clicked,
instead of also being triggered when it is created/initialized. Similarly,
if you're setting up an <code>eventReactive</code> that responds to a dynamically
created button used to refresh some data (then returned by that <code>eventReactive</code>),
then you should use <code>eventReactive([...], ignoreInit = TRUE)</code> if you want
to let the user decide if/when they want to refresh the data (since, depending
on the app, this may be a computationally expensive operation).</p>
<p>Even though <code>ignoreNULL</code> and <code>ignoreInit</code> can be used for similar
purposes they are independent from one another. Here's the result of combining
these:</p>
<dl><dt><code>ignoreNULL = TRUE</code> and <code>ignoreInit = FALSE</code></dt>
<dd><p>This is the default. This combination means that <code>handlerExpr</code>/
<code>valueExpr</code> will run every time that <code>eventExpr</code> is not
<code>NULL</code>. If, at the time of the creation of the
<code>observeEvent</code>/<code>eventReactive</code>, <code>eventExpr</code> happens
to <em>not</em> be <code>NULL</code>, then the code runs.</p></dd>
<dt><code>ignoreNULL = FALSE</code> and <code>ignoreInit = FALSE</code></dt>
<dd><p>This combination means that <code>handlerExpr</code>/<code>valueExpr</code> will
run every time no matter what.</p></dd>
<dt><code>ignoreNULL = FALSE</code> and <code>ignoreInit = TRUE</code></dt>
<dd><p>This combination means that <code>handlerExpr</code>/<code>valueExpr</code> will
<em>not</em> run when the <code>observeEvent</code>/<code>eventReactive</code> is
created (because <code>ignoreInit = TRUE</code>), but it will run every
other time.</p></dd>
<dt><code>ignoreNULL = TRUE</code> and <code>ignoreInit = TRUE</code></dt>
<dd><p>This combination means that <code>handlerExpr</code>/<code>valueExpr</code> will
<em>not</em> run when the <code>observeEvent</code>/<code>eventReactive</code> is
created (because <code>ignoreInit = TRUE</code>). After that,
<code>handlerExpr</code>/<code>valueExpr</code> will run every time that
<code>eventExpr</code> is not <code>NULL</code>.</p></dd>
</dl></div>
<div id="see-also">
<h2>See also</h2>
<div class="dont-index"><p><code><a href="actionButton.html">actionButton()</a></code></p></div>
</div>
<div id="ref-examples">
<h2>Examples</h2>
<div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><span><span class="co">## Only run examples in interactive R sessions</span></span></span>
<span class="r-in"><span><span class="kw">if</span> <span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/interactive.html" class="external-link">interactive</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span> <span class="op">{</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span> <span class="co">## App 1: Sample usage</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="shinyApp.html">shinyApp</a></span><span class="op">(</span></span></span>
<span class="r-in"><span> ui <span class="op">=</span> <span class="fu"><a href="fluidPage.html">fluidPage</a></span><span class="op">(</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="column.html">column</a></span><span class="op">(</span><span class="fl">4</span>,</span></span>
<span class="r-in"><span> <span class="fu"><a href="numericInput.html">numericInput</a></span><span class="op">(</span><span class="st">"x"</span>, <span class="st">"Value"</span>, <span class="fl">5</span><span class="op">)</span>,</span></span>
<span class="r-in"><span> <span class="fu"><a href="https://rstudio.github.io/htmltools/reference/builder.html" class="external-link">br</a></span><span class="op">(</span><span class="op">)</span>,</span></span>
<span class="r-in"><span> <span class="fu"><a href="actionButton.html">actionButton</a></span><span class="op">(</span><span class="st">"button"</span>, <span class="st">"Show"</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">)</span>,</span></span>
<span class="r-in"><span> <span class="fu"><a href="column.html">column</a></span><span class="op">(</span><span class="fl">8</span>, <span class="fu"><a href="renderTable.html">tableOutput</a></span><span class="op">(</span><span class="st">"table"</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">)</span>,</span></span>
<span class="r-in"><span> server <span class="op">=</span> <span class="kw">function</span><span class="op">(</span><span class="va">input</span>, <span class="va">output</span><span class="op">)</span> <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="co"># Take an action every time button is pressed;</span></span></span>
<span class="r-in"><span> <span class="co"># here, we just print a message to the console</span></span></span>
<span class="r-in"><span> <span class="fu">observeEvent</span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">button</span>, <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="https://rdrr.io/r/base/cat.html" class="external-link">cat</a></span><span class="op">(</span><span class="st">"Showing"</span>, <span class="va">input</span><span class="op">$</span><span class="va">x</span>, <span class="st">"rows\n"</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">}</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="co"># The observeEvent() above is equivalent to:</span></span></span>
<span class="r-in"><span> <span class="co"># observe({</span></span></span>
<span class="r-in"><span> <span class="co"># cat("Showing", input$x, "rows\n")</span></span></span>
<span class="r-in"><span> <span class="co"># }) %&gt;%</span></span></span>
<span class="r-in"><span> <span class="co"># bindEvent(input$button)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span> <span class="co"># Take a reactive dependency on input$button, but</span></span></span>
<span class="r-in"><span> <span class="co"># not on any of the stuff inside the function</span></span></span>
<span class="r-in"><span> <span class="va">df</span> <span class="op">&lt;-</span> <span class="fu">eventReactive</span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">button</span>, <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">cars</span>, <span class="va">input</span><span class="op">$</span><span class="va">x</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">}</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="va">output</span><span class="op">$</span><span class="va">table</span> <span class="op">&lt;-</span> <span class="fu"><a href="renderTable.html">renderTable</a></span><span class="op">(</span><span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="https://rdrr.io/r/stats/Fdist.html" class="external-link">df</a></span><span class="op">(</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">}</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">}</span></span></span>
<span class="r-in"><span> <span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span> <span class="co">## App 2: Using `once`</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="shinyApp.html">shinyApp</a></span><span class="op">(</span></span></span>
<span class="r-in"><span> ui <span class="op">=</span> <span class="fu"><a href="bootstrapPage.html">basicPage</a></span><span class="op">(</span> <span class="fu"><a href="actionButton.html">actionButton</a></span><span class="op">(</span><span class="st">"go"</span>, <span class="st">"Go"</span><span class="op">)</span><span class="op">)</span>,</span></span>
<span class="r-in"><span> server <span class="op">=</span> <span class="kw">function</span><span class="op">(</span><span class="va">input</span>, <span class="va">output</span>, <span class="va">session</span><span class="op">)</span> <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu">observeEvent</span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">go</span>, <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="https://rdrr.io/r/base/print.html" class="external-link">print</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste</a></span><span class="op">(</span><span class="st">"This will only be printed once; all"</span>,</span></span>
<span class="r-in"><span> <span class="st">"subsequent button clicks won't do anything"</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">}</span>, once <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="co"># The observeEvent() above is equivalent to:</span></span></span>
<span class="r-in"><span> <span class="co"># observe({</span></span></span>
<span class="r-in"><span> <span class="co"># print(paste("This will only be printed once; all",</span></span></span>
<span class="r-in"><span> <span class="co"># "subsequent button clicks won't do anything"))</span></span></span>
<span class="r-in"><span> <span class="co"># }) %&gt;%</span></span></span>
<span class="r-in"><span> <span class="co"># bindEvent(input$go, once = TRUE)</span></span></span>
<span class="r-in"><span> <span class="op">}</span></span></span>
<span class="r-in"><span> <span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span> <span class="co">## App 3: Using `ignoreInit` and `once`</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="shinyApp.html">shinyApp</a></span><span class="op">(</span></span></span>
<span class="r-in"><span> ui <span class="op">=</span> <span class="fu"><a href="bootstrapPage.html">basicPage</a></span><span class="op">(</span><span class="fu"><a href="actionButton.html">actionButton</a></span><span class="op">(</span><span class="st">"go"</span>, <span class="st">"Go"</span><span class="op">)</span><span class="op">)</span>,</span></span>
<span class="r-in"><span> server <span class="op">=</span> <span class="kw">function</span><span class="op">(</span><span class="va">input</span>, <span class="va">output</span>, <span class="va">session</span><span class="op">)</span> <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu">observeEvent</span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">go</span>, <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="insertUI.html">insertUI</a></span><span class="op">(</span><span class="st">"#go"</span>, <span class="st">"afterEnd"</span>,</span></span>
<span class="r-in"><span> <span class="fu"><a href="actionButton.html">actionButton</a></span><span class="op">(</span><span class="st">"dynamic"</span>, <span class="st">"click to remove"</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span> <span class="co"># set up an observer that depends on the dynamic</span></span></span>
<span class="r-in"><span> <span class="co"># input, so that it doesn't run when the input is</span></span></span>
<span class="r-in"><span> <span class="co"># created, and only runs once after that (since</span></span></span>
<span class="r-in"><span> <span class="co"># the side effect is remove the input from the DOM)</span></span></span>
<span class="r-in"><span> <span class="fu">observeEvent</span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">dynamic</span>, <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="insertUI.html">removeUI</a></span><span class="op">(</span><span class="st">"#dynamic"</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">}</span>, ignoreInit <span class="op">=</span> <span class="cn">TRUE</span>, once <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">}</span><span class="op">)</span></span></span>
<span class="r-in"><span> <span class="op">}</span></span></span>
<span class="r-in"><span> <span class="op">)</span></span></span>
<span class="r-in"><span><span class="op">}</span></span></span>
</code></pre></div>
</div>
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
<nav id="toc" data-toggle="toc" class="sticky-top"><h2 data-toc-skip>Contents</h2>
</nav></div>
</div>
<footer><div class="copyright">
<p></p><p>Developed by Winston Chang, Joe Cheng, JJ Allaire, Carson Sievert, Barret Schloerke, Garrick Aden-Buie, Yihui Xie, Jeff Allen, Jonathan McPherson, Alan Dipert, Barbara Borges, Posit Software, PBC.</p>
</div>
<div class="pkgdown">
<p></p><p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.2.0.</p>
</div>
</footer></div>
</body></html>