Files
shiny/reference/req.html
2022-10-21 19:53:05 +00:00

189 lines
15 KiB
HTML

<!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>Check for required values — req • shiny</title><!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png"><link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png"><link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png"><link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png"><link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png"><!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></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="Check for required values — req"><meta property="og:description" content="Ensure that values are available (&quot;truthy&quot;) before proceeding
with a calculation or action. If any of the given values is not truthy, the
operation is stopped by raising a &quot;silent&quot; exception (not logged by Shiny,
nor displayed in the Shiny app's UI)."><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.7.2</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>Check for required values</h1>
<small class="dont-index">Source: <a href="https://github.com/rstudio/shiny/blob/HEAD/R/utils.R" class="external-link"><code>R/utils.R</code></a></small>
<div class="hidden name"><code>req.Rd</code></div>
</div>
<div class="ref-description">
<p>Ensure that values are available (<a href="isTruthy.html">"truthy"</a>) before proceeding
with a calculation or action. If any of the given values is not truthy, the
operation is stopped by raising a "silent" exception (not logged by Shiny,
nor displayed in the Shiny app's UI).</p>
</div>
<div id="ref-usage">
<div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">req</span><span class="op">(</span><span class="va">...</span>, cancelOutput <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="arguments">
<h2>Arguments</h2>
<dl><dt>...</dt>
<dd><p>Values to check for truthiness.</p></dd>
<dt>cancelOutput</dt>
<dd><p>If <code>TRUE</code> and an output is being evaluated, stop
processing as usual but instead of clearing the output, leave it in
whatever state it happens to be in.</p></dd>
</dl></div>
<div id="value">
<h2>Value</h2>
<p>The first value that was passed in.</p>
</div>
<div id="details">
<h2>Details</h2>
<p>The <code>req</code> function was designed to be used in one of two ways. The first
is to call it like a statement (ignoring its return value) before attempting
operations using the required values:</p>
<p></p><div class="sourceCode"><pre><code><span id="cb1-1"><a href="#cb1-1"></a>rv &lt;-<span class="st"> </span><span class="kw">reactiveValues</span>(<span class="dt">state =</span> <span class="ot">FALSE</span>)</span>
<span id="cb1-2"><a href="#cb1-2"></a>r &lt;-<span class="st"> </span><span class="kw">reactive</span>({</span>
<span id="cb1-3"><a href="#cb1-3"></a> <span class="kw">req</span>(input<span class="op">$</span>a, input<span class="op">$</span>b, rv<span class="op">$</span>state)</span>
<span id="cb1-4"><a href="#cb1-4"></a> <span class="co"># Code that uses input$a, input$b, and/or rv$state...</span></span>
<span id="cb1-5"><a href="#cb1-5"></a>})</span></code></pre><p></p></div>
<p>In this example, if <code>r()</code> is called and any of <code>input$a</code>,
<code>input$b</code>, and <code>rv$state</code> are <code>NULL</code>, <code>FALSE</code>, <code>""</code>,
etc., then the <code>req</code> call will trigger an error that propagates all the
way up to whatever render block or observer is executing.</p>
<p>The second is to use it to wrap an expression that must be truthy:</p>
<p></p><div class="sourceCode"><pre><code><span id="cb1-1"><a href="#cb1-1"></a>output<span class="op">$</span>plot &lt;-<span class="st"> </span><span class="kw">renderPlot</span>({</span>
<span id="cb1-2"><a href="#cb1-2"></a> <span class="cf">if</span> (<span class="kw">req</span>(input<span class="op">$</span>plotType) <span class="op">==</span><span class="st"> "histogram"</span>) {</span>
<span id="cb1-3"><a href="#cb1-3"></a> <span class="kw">hist</span>(<span class="kw">dataset</span>())</span>
<span id="cb1-4"><a href="#cb1-4"></a> } <span class="cf">else</span> <span class="cf">if</span> (input<span class="op">$</span>plotType <span class="op">==</span><span class="st"> "scatter"</span>) {</span>
<span id="cb1-5"><a href="#cb1-5"></a> <span class="kw">qplot</span>(<span class="kw">dataset</span>(), <span class="kw">aes</span>(<span class="dt">x =</span> x, <span class="dt">y =</span> y))</span>
<span id="cb1-6"><a href="#cb1-6"></a> }</span>
<span id="cb1-7"><a href="#cb1-7"></a>})</span></code></pre><p></p></div>
<p>In this example, <code>req(input$plotType)</code> first checks that
<code>input$plotType</code> is truthy, and if so, returns it. This is a convenient
way to check for a value "inline" with its first use.</p>
</div>
<div id="using-req-false-">
<h2>Using <code>req(FALSE)</code></h2>
<p>You can use <code>req(FALSE)</code> (i.e. no condition) if you've already performed
all the checks you needed to by that point and just want to stop the reactive
chain now. There is no advantange to this, except perhaps ease of readibility
if you have a complicated condition to check for (or perhaps if you'd like to
divide your condition into nested <code>if</code> statements).</p>
</div>
<div id="using-canceloutput-true">
<h2>Using <code>cancelOutput = TRUE</code></h2>
<p>When <code>req(..., cancelOutput = TRUE)</code> is used, the "silent" exception is
also raised, but it is treated slightly differently if one or more outputs are
currently being evaluated. In those cases, the reactive chain does not proceed
or update, but the output(s) are left is whatever state they happen to be in
(whatever was their last valid state).</p>
<p>Note that this is always going to be the case if
this is used inside an output context (e.g. <code>output$txt &lt;- ...</code>). It may
or may not be the case if it is used inside a non-output context (e.g.
<code><a href="reactive.html">reactive()</a></code>, <code><a href="observe.html">observe()</a></code> or <code><a href="observeEvent.html">observeEvent()</a></code>)
--- depending on whether or not there is an <code>output$...</code> that is triggered
as a result of those calls. See the examples below for concrete scenarios.</p>
</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 class="va">ui</span> <span class="op">&lt;-</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="textInput.html">textInput</a></span><span class="op">(</span><span class="st">'data'</span>, <span class="st">'Enter a dataset from the "datasets" package'</span>, <span class="st">'cars'</span><span class="op">)</span>,</span></span>
<span class="r-in"><span> <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html" class="external-link">p</a></span><span class="op">(</span><span class="st">'(E.g. "cars", "mtcars", "pressure", "faithful")'</span><span class="op">)</span>, <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html" class="external-link">hr</a></span><span class="op">(</span><span class="op">)</span>,</span></span>
<span class="r-in"><span> <span class="fu"><a href="renderTable.html">tableOutput</a></span><span class="op">(</span><span class="st">'tbl'</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="va">server</span> <span class="op">&lt;-</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="va">output</span><span class="op">$</span><span class="va">tbl</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></span>
<span class="r-in"><span> <span class="co">## to require that the user types something, use: `req(input$data)`</span></span></span>
<span class="r-in"><span> <span class="co">## but better: require that input$data is valid and leave the last</span></span></span>
<span class="r-in"><span> <span class="co">## valid table up</span></span></span>
<span class="r-in"><span> <span class="fu">req</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/exists.html" class="external-link">exists</a></span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">data</span>, <span class="st">"package:datasets"</span>, inherits <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span>,</span></span>
<span class="r-in"><span> cancelOutput <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="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/get.html" class="external-link">get</a></span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">data</span>, <span class="st">"package:datasets"</span>, inherits <span class="op">=</span> <span class="cn">FALSE</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></span>
<span class="r-in"><span> <span class="fu"><a href="shinyApp.html">shinyApp</a></span><span class="op">(</span><span class="va">ui</span>, <span class="va">server</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, Yihui Xie, Jeff Allen, Jonathan McPherson, Alan Dipert, Barbara Borges.</p>
</div>
<div class="pkgdown">
<p></p><p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.6.</p>
</div>
</footer></div>
</body></html>