Files
shiny/reference/insertUI.html
2025-12-03 21:27:07 +00:00

228 lines
17 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>Insert and remove UI objects — insertUI • 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="Insert and remove UI objects — insertUI"><meta property="og:description" content="These functions allow you to dynamically add and remove arbitrary UI
into your app, whenever you want, as many times as you want.
Unlike renderUI(), the UI generated with insertUI() is persistent:
once it's created, it stays there until removed by removeUI(). Each
new call to insertUI() creates more UI objects, in addition to
the ones already there (all independent from one another). To
update a part of the UI (ex: an input object), you must use the
appropriate render function or a customized reactive
function."><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>Insert and remove UI objects</h1>
<small class="dont-index">Source: <a href="https://github.com/rstudio/shiny/blob/rc-v1.12.1/R/insert-ui.R" class="external-link"><code>R/insert-ui.R</code></a></small>
<div class="hidden name"><code>insertUI.Rd</code></div>
</div>
<div class="ref-description">
<p>These functions allow you to dynamically add and remove arbitrary UI
into your app, whenever you want, as many times as you want.
Unlike <code><a href="renderUI.html">renderUI()</a></code>, the UI generated with <code>insertUI()</code> is persistent:
once it's created, it stays there until removed by <code>removeUI()</code>. Each
new call to <code>insertUI()</code> creates more UI objects, in addition to
the ones already there (all independent from one another). To
update a part of the UI (ex: an input object), you must use the
appropriate <code>render</code> function or a customized <code>reactive</code>
function.</p>
</div>
<div id="ref-usage">
<div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">insertUI</span><span class="op">(</span></span>
<span> <span class="va">selector</span>,</span>
<span> where <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"beforeBegin"</span>, <span class="st">"afterBegin"</span>, <span class="st">"beforeEnd"</span>, <span class="st">"afterEnd"</span><span class="op">)</span>,</span>
<span> <span class="va">ui</span>,</span>
<span> multiple <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> immediate <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> session <span class="op">=</span> <span class="fu"><a href="domains.html">getDefaultReactiveDomain</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="fu">removeUI</span><span class="op">(</span></span>
<span> <span class="va">selector</span>,</span>
<span> multiple <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> immediate <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> session <span class="op">=</span> <span class="fu"><a href="domains.html">getDefaultReactiveDomain</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="op">)</span></span></code></pre></div>
</div>
<div id="arguments">
<h2>Arguments</h2>
<dl><dt id="arg-selector">selector<a class="anchor" aria-label="anchor" href="#arg-selector"></a></dt>
<dd><p>A string that is accepted by jQuery's selector
(i.e. the string <code>s</code> to be placed in a <code>$(s)</code> jQuery call).</p>
<p>For <code>insertUI()</code> this determines the element(s) relative to which you
want to insert your UI object. For <code>removeUI()</code> this determine the
element(s) to be removed. If you want to remove a Shiny input or output,
note that many of these are wrapped in <code>&lt;div&gt;</code>s, so you may need to use a
somewhat complex selector — see the Examples below. (Alternatively, you
could also wrap the inputs/outputs that you want to be able to remove
easily in a <code>&lt;div&gt;</code> with an id.)</p></dd>
<dt id="arg-where">where<a class="anchor" aria-label="anchor" href="#arg-where"></a></dt>
<dd><p>Where your UI object should go relative to the selector:</p><dl><dt><code>beforeBegin</code></dt>
<dd><p>Before the selector element itself</p></dd>
<dt><code>afterBegin</code></dt>
<dd><p>Just inside the selector element, before its
first child</p></dd>
<dt><code>beforeEnd</code></dt>
<dd><p>Just inside the selector element, after its
last child (default)</p></dd>
<dt><code>afterEnd</code></dt>
<dd><p>After the selector element itself</p></dd>
</dl><p>Adapted from <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML" class="external-link">https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML</a>.</p></dd>
<dt id="arg-ui">ui<a class="anchor" aria-label="anchor" href="#arg-ui"></a></dt>
<dd><p>The UI object you want to insert. This can be anything that
you usually put inside your apps's <code>ui</code> function. If you're inserting
multiple elements in one call, make sure to wrap them in either a
<code><a href="https://rstudio.github.io/htmltools/reference/tagList.html" class="external-link">tagList()</a></code> or a <code>tags$div()</code> (the latter option has the
advantage that you can give it an <code>id</code> to make it easier to
reference or remove it later on). If you want to insert raw html, use
<code>ui = HTML()</code>.</p></dd>
<dt id="arg-multiple">multiple<a class="anchor" aria-label="anchor" href="#arg-multiple"></a></dt>
<dd><p>In case your selector matches more than one element,
<code>multiple</code> determines whether Shiny should insert the UI object
relative to all matched elements or just relative to the first
matched element (default).</p></dd>
<dt id="arg-immediate">immediate<a class="anchor" aria-label="anchor" href="#arg-immediate"></a></dt>
<dd><p>Whether the UI object should be immediately inserted
or removed, or whether Shiny should wait until all outputs have been
updated and all observers have been run (default).</p></dd>
<dt id="arg-session">session<a class="anchor" aria-label="anchor" href="#arg-session"></a></dt>
<dd><p>The shiny session. Advanced use only.</p></dd>
</dl></div>
<div id="details">
<h2>Details</h2>
<p>It's particularly useful to pair <code>removeUI</code> with <code>insertUI()</code>, but there is
no restriction on what you can use it on. Any element that can be selected
through a jQuery selector can be removed through this function.</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 this example 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="co"># Define UI</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="actionButton.html">actionButton</a></span><span class="op">(</span><span class="st">"add"</span>, <span class="st">"Add UI"</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"># Server logic</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="va">session</span><span class="op">)</span> <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="observeEvent.html">observeEvent</a></span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">add</span>, <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu">insertUI</span><span class="op">(</span></span></span>
<span class="r-in"><span> selector <span class="op">=</span> <span class="st">"#add"</span>,</span></span>
<span class="r-in"><span> where <span class="op">=</span> <span class="st">"afterEnd"</span>,</span></span>
<span class="r-in"><span> ui <span class="op">=</span> <span class="fu"><a href="textInput.html">textInput</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste0</a></span><span class="op">(</span><span class="st">"txt"</span>, <span class="va">input</span><span class="op">$</span><span class="va">add</span><span class="op">)</span>,</span></span>
<span class="r-in"><span> <span class="st">"Insert some text"</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 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"># Complete app with UI and server components</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>
<span class="r-in"><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="co"># Define UI</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="actionButton.html">actionButton</a></span><span class="op">(</span><span class="st">"rmv"</span>, <span class="st">"Remove UI"</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">"txt"</span>, <span class="st">"This is no longer useful"</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"># Server logic</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="va">session</span><span class="op">)</span> <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu"><a href="observeEvent.html">observeEvent</a></span><span class="op">(</span><span class="va">input</span><span class="op">$</span><span class="va">rmv</span>, <span class="op">{</span></span></span>
<span class="r-in"><span> <span class="fu">removeUI</span><span class="op">(</span></span></span>
<span class="r-in"><span> selector <span class="op">=</span> <span class="st">"div:has(&gt; #txt)"</span></span></span>
<span class="r-in"><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="co"># Complete app with UI and server components</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.2.0.</p>
</div>
</footer></div>
</body></html>