Files
shiny/reference/observe.html
2025-12-03 21:39:55 +00:00

231 lines
16 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>Create a reactive observer — observe • 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="Create a reactive observer — observe"><meta property="og:description" content="Creates an observer from the given expression."><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.0.9000</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>Create a reactive observer</h1>
<small class="dont-index">Source: <a href="https://github.com/rstudio/shiny/blob/main/R/reactives.R" class="external-link"><code>R/reactives.R</code></a></small>
<div class="hidden name"><code>observe.Rd</code></div>
</div>
<div class="ref-description">
<p>Creates an observer from the given expression.</p>
</div>
<div id="ref-usage">
<div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">observe</span><span class="op">(</span></span>
<span> <span class="va">x</span>,</span>
<span> 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> 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> ..stacktraceon <span class="op">=</span> <span class="cn">TRUE</span></span>
<span><span class="op">)</span></span></code></pre></div>
</div>
<div id="arguments">
<h2>Arguments</h2>
<dl><dt id="arg-x">x<a class="anchor" aria-label="anchor" href="#arg-x"></a></dt>
<dd><p>An expression (quoted or unquoted). Any return value will be
ignored.</p></dd>
<dt id="arg-env">env<a class="anchor" aria-label="anchor" href="#arg-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>x</code> is a quosure and <code>quoted</code> is <code>TRUE</code>,
then <code>env</code> is ignored.</p></dd>
<dt id="arg-quoted">quoted<a class="anchor" aria-label="anchor" href="#arg-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>x</code>
will be used when <code>x</code> is evaluated. If <code>x</code> is a quosure and you
would like to use its expression as a value for <code>x</code>, then you must set
<code>quoted</code> to <code>TRUE</code>.</p></dd>
<dt id="arg--">...<a class="anchor" aria-label="anchor" href="#arg--"></a></dt>
<dd><p>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, 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. A higher value means higher priority: an
observer with a higher priority value will execute before all observers
with lower priority values. 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--stacktraceon">..stacktraceon<a class="anchor" aria-label="anchor" href="#arg--stacktraceon"></a></dt>
<dd><p>Advanced use only. For stack manipulation purposes; see
<code><a href="stacktrace.html">stacktrace()</a></code>.</p></dd>
</dl></div>
<div id="value">
<h2>Value</h2>
<p>An observer reference class object. This object has the following
methods:</p><dl><dt><code>suspend()</code></dt>
<dd><p>Causes this observer to stop scheduling flushes (re-executions) in
response to invalidations. If the observer was invalidated prior to
this call but it has not re-executed yet then that re-execution will
still occur, because the flush is already scheduled.</p></dd>
<dt><code>resume()</code></dt>
<dd><p>Causes this observer to start re-executing in response to
invalidations. If the observer was invalidated while suspended, then it
will schedule itself for re-execution.</p></dd>
<dt><code>destroy()</code></dt>
<dd><p>Stops the observer from executing ever again, even if it is currently
scheduled for re-execution.</p></dd>
<dt><code>setPriority(priority = 0)</code></dt>
<dd><p>Change this observer's priority. Note that if the observer is currently
invalidated, then the change in priority will not take effect until the
next invalidationunless the observer is also currently suspended, in
which case the priority change will be effective upon resume.</p></dd>
<dt><code>setAutoDestroy(autoDestroy)</code></dt>
<dd><p>Sets whether this observer should be automatically destroyed when its
domain (if any) ends. If autoDestroy is TRUE and the domain already
ended, then destroy() is called immediately."</p></dd>
<dt><code>onInvalidate(callback)</code></dt>
<dd><p>Register a callback function to run when this observer is invalidated.
No arguments will be provided to the callback function when it is
invoked.</p></dd>
</dl></div>
<div id="details">
<h2>Details</h2>
<p>An observer is like a reactive expression in that it can read reactive values
and call reactive expressions, and will automatically re-execute when those
dependencies change. But unlike reactive expressions, it doesn't yield a
result and can't be used as an input to other reactive expressions. Thus,
observers are only useful for their side effects (for example, performing
I/O).</p>
<p>Another contrast between reactive expressions and observers is their
execution strategy. Reactive expressions use lazy evaluation; that is, when
their dependencies change, they don't re-execute right away but rather wait
until they are called by someone else. Indeed, if they are not called then
they will never re-execute. In contrast, observers use eager evaluation; as
soon as their dependencies change, they schedule themselves to re-execute.</p>
<p>Starting with Shiny 0.10.0, observers are automatically destroyed by default
when the <a href="domains.html">domain</a> that owns them ends (e.g. when a Shiny
session ends).</p>
</div>
<div id="ref-examples">
<h2>Examples</h2>
<div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><span><span class="va">values</span> <span class="op">&lt;-</span> <span class="fu"><a href="reactiveValues.html">reactiveValues</a></span><span class="op">(</span>A<span class="op">=</span><span class="fl">1</span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="va">obsB</span> <span class="op">&lt;-</span> <span class="fu">observe</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/base/print.html" class="external-link">print</a></span><span class="op">(</span><span class="va">values</span><span class="op">$</span><span class="va">A</span> <span class="op">+</span> <span class="fl">1</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></span>
<span class="r-in"><span><span class="co"># To store expressions for later conversion to observe, use rlang::quo()</span></span></span>
<span class="r-in"><span><span class="va">myquo</span> <span class="op">&lt;-</span> <span class="fu">rlang</span><span class="fu">::</span><span class="fu"><a href="https://rlang.r-lib.org/reference/defusing-advanced.html" class="external-link">quo</a></span><span class="op">(</span><span class="op">{</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="va">values</span><span class="op">$</span><span class="va">A</span> <span class="op">+</span> <span class="fl">3</span><span class="op">)</span> <span class="op">}</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="va">obsC</span> <span class="op">&lt;-</span> <span class="fu">rlang</span><span class="fu">::</span><span class="fu"><a href="https://rlang.r-lib.org/reference/inject.html" class="external-link">inject</a></span><span class="op">(</span><span class="fu">observe</span><span class="op">(</span><span class="op">!</span><span class="op">!</span><span class="va">myquo</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"># (Legacy) Can use quoted expressions</span></span></span>
<span class="r-in"><span><span class="va">obsD</span> <span class="op">&lt;-</span> <span class="fu">observe</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/substitute.html" class="external-link">quote</a></span><span class="op">(</span><span class="op">{</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="va">values</span><span class="op">$</span><span class="va">A</span> <span class="op">+</span> <span class="fl">2</span><span class="op">)</span> <span class="op">}</span><span class="op">)</span>, quoted <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># In a normal Shiny app, the web client will trigger flush events. If you</span></span></span>
<span class="r-in"><span><span class="co"># are at the console, you can force a flush with flushReact()</span></span></span>
<span class="r-in"><span><span class="fu">shiny</span><span class="fu">:::</span><span class="fu">flushReact</span><span class="op">(</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] -30</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] 2</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] 4</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] 3</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>