mirror of
https://github.com/rstudio/shiny.git
synced 2026-04-29 03:00:45 -04:00
192 lines
15 KiB
HTML
192 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><!-- 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="Check for required values — req"><meta property="og:description" content="Ensure that values are available ("truthy") 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)."><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.11.1.9001</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/main/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 id="arg--">...<a class="anchor" aria-label="anchor" href="#arg--"></a></dt>
|
|
<dd><p>Values to check for truthiness.</p></dd>
|
|
|
|
|
|
<dt id="arg-canceloutput">cancelOutput<a class="anchor" aria-label="anchor" href="#arg-canceloutput"></a></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. If <code>"progress"</code>, do the same as <code>TRUE</code>,
|
|
but also keep the output in recalculating state; this is intended for cases
|
|
when an in-progress calculation will not be completed in this reactive
|
|
flush cycle, but is still expected to provide a result in the future.</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" tabindex="-1"></a>rv <span class="ot"><-</span> <span class="fu">reactiveValues</span>(<span class="at">state =</span> <span class="cn">FALSE</span>)</span>
|
|
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a>r <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
|
|
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>a, input<span class="sc">$</span>b, rv<span class="sc">$</span>state)</span>
|
|
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></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" tabindex="-1"></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" tabindex="-1"></a>output<span class="sc">$</span>plot <span class="ot"><-</span> <span class="fu">renderPlot</span>({</span>
|
|
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">req</span>(input<span class="sc">$</span>plotType) <span class="sc">==</span> <span class="st">"histogram"</span>) {</span>
|
|
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a> <span class="fu">hist</span>(<span class="fu">dataset</span>())</span>
|
|
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a> } <span class="cf">else</span> <span class="cf">if</span> (input<span class="sc">$</span>plotType <span class="sc">==</span> <span class="st">"scatter"</span>) {</span>
|
|
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a> <span class="fu">qplot</span>(<span class="fu">dataset</span>(), <span class="fu">aes</span>(<span class="at">x =</span> x, <span class="at">y =</span> y))</span>
|
|
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a> }</span>
|
|
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></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 advantage to this, except perhaps ease of readability
|
|
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 <- ...</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"><-</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://rstudio.github.io/htmltools/reference/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://rstudio.github.io/htmltools/reference/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"><-</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"><-</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, 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.1.3.</p>
|
|
</div>
|
|
|
|
</footer></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</body></html>
|
|
|