Debian Codegraph @

inst/doc/shinyjs-example.html

raw
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13 <meta name="author" content="Dean Attali" />
14
15 <meta name="date" content="2026-01-14" />
16
17 <title>shinyjs example app walk-through</title>
18
19 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
20 // be compatible with the behavior of Pandoc < 2.8).
21 document.addEventListener('DOMContentLoaded', function(e) {
22 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
23 var i, h, a;
24 for (i = 0; i < hs.length; i++) {
25 h = hs[i];
26 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
27 a = h.attributes;
28 while (a.length > 0) h.removeAttribute(a[0].name);
29 }
30 });
31 </script>
32
33 <style type="text/css">
34 code{white-space: pre-wrap;}
35 span.smallcaps{font-variant: small-caps;}
36 span.underline{text-decoration: underline;}
37 div.column{display: inline-block; vertical-align: top; width: 50%;}
38 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
39 ul.task-list{list-style: none;}
40 </style>
41
42
43
44
45
46
47
48 <style type="text/css">body {
49 background-color: #fff;
50 margin: 1em auto;
51 max-width: 700px;
52 overflow: visible;
53 padding-left: 2em;
54 padding-right: 2em;
55 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
56 font-size: 14px;
57 line-height: 1.35;
58 }
59 #TOC {
60 clear: both;
61 margin: 0 0 10px 10px;
62 padding: 4px;
63 width: 400px;
64 border: 1px solid #CCCCCC;
65 border-radius: 5px;
66 background-color: #f6f6f6;
67 font-size: 13px;
68 line-height: 1.3;
69 }
70 #TOC .toctitle {
71 font-weight: bold;
72 font-size: 15px;
73 margin-left: 5px;
74 }
75 #TOC ul {
76 padding-left: 40px;
77 margin-left: -1.5em;
78 margin-top: 5px;
79 margin-bottom: 5px;
80 }
81 #TOC ul ul {
82 margin-left: -2em;
83 }
84 #TOC li {
85 line-height: 16px;
86 }
87 table {
88 margin: 1em auto;
89 border-width: 1px;
90 border-color: #DDDDDD;
91 border-style: outset;
92 border-collapse: collapse;
93 }
94 table th {
95 border-width: 2px;
96 padding: 5px;
97 border-style: inset;
98 }
99 table td {
100 border-width: 1px;
101 border-style: inset;
102 line-height: 18px;
103 padding: 5px 5px;
104 }
105 table, table th, table td {
106 border-left-style: none;
107 border-right-style: none;
108 }
109 table thead, table tr.even {
110 background-color: #f7f7f7;
111 }
112 p {
113 margin: 0.5em 0;
114 }
115 blockquote {
116 background-color: #f6f6f6;
117 padding: 0.25em 0.75em;
118 }
119 hr {
120 border-style: solid;
121 border: none;
122 border-top: 1px solid #777;
123 margin: 28px 0;
124 }
125 dl {
126 margin-left: 0;
127 }
128 dl dd {
129 margin-bottom: 13px;
130 margin-left: 13px;
131 }
132 dl dt {
133 font-weight: bold;
134 }
135 ul {
136 margin-top: 0;
137 }
138 ul li {
139 list-style: circle outside;
140 }
141 ul ul {
142 margin-bottom: 0;
143 }
144 pre, code {
145 background-color: #f7f7f7;
146 border-radius: 3px;
147 color: #333;
148 white-space: pre-wrap;
149 }
150 pre {
151 border-radius: 3px;
152 margin: 5px 0px 10px 0px;
153 padding: 10px;
154 }
155 pre:not([class]) {
156 background-color: #f7f7f7;
157 }
158 code {
159 font-family: Consolas, Monaco, 'Courier New', monospace;
160 font-size: 85%;
161 }
162 p > code, li > code {
163 padding: 2px 0px;
164 }
165 div.figure {
166 text-align: center;
167 }
168 img {
169 background-color: #FFFFFF;
170 padding: 2px;
171 border: 1px solid #DDDDDD;
172 border-radius: 3px;
173 border: 1px solid #CCCCCC;
174 margin: 0 5px;
175 }
176 h1 {
177 margin-top: 0;
178 font-size: 35px;
179 line-height: 40px;
180 }
181 h2 {
182 border-bottom: 4px solid #f7f7f7;
183 padding-top: 10px;
184 padding-bottom: 2px;
185 font-size: 145%;
186 }
187 h3 {
188 border-bottom: 2px solid #f7f7f7;
189 padding-top: 10px;
190 font-size: 120%;
191 }
192 h4 {
193 border-bottom: 1px solid #f7f7f7;
194 margin-left: 8px;
195 font-size: 105%;
196 }
197 h5, h6 {
198 border-bottom: 1px solid #ccc;
199 font-size: 105%;
200 }
201 a {
202 color: #0033dd;
203 text-decoration: none;
204 }
205 a:hover {
206 color: #6666ff; }
207 a:visited {
208 color: #800080; }
209 a:visited:hover {
210 color: #BB00BB; }
211 a[href^="http:"] {
212 text-decoration: underline; }
213 a[href^="https:"] {
214 text-decoration: underline; }
215
216 code > span.kw { color: #555; font-weight: bold; }
217 code > span.dt { color: #902000; }
218 code > span.dv { color: #40a070; }
219 code > span.bn { color: #d14; }
220 code > span.fl { color: #d14; }
221 code > span.ch { color: #d14; }
222 code > span.st { color: #d14; }
223 code > span.co { color: #888888; font-style: italic; }
224 code > span.ot { color: #007020; }
225 code > span.al { color: #ff0000; font-weight: bold; }
226 code > span.fu { color: #900; font-weight: bold; }
227 code > span.er { color: #a61717; background-color: #e3d2d2; }
228 </style>
229
230
231
232
233 </head>
234
235 <body>
236
237
238
239
240 <h1 class="title toc-ignore">shinyjs example app walk-through</h1>
241 <h4 class="author">Dean Attali</h4>
242 <h4 class="date">2026-01-14</h4>
243
244
245
246 <div id="shinyjs-example-app-walk-through" class="section level1">
247 <h1>shinyjs example app walk-through</h1>
248 <p>This document provides a step-by-step guide on how to add a variety
249 of shinyjs features to a simple app in order to make it more user
250 friendly.</p>
251 <p><em>You can view the final Shiny app developed in this simple example
252 <a href="https://daattali.com/shiny/shinyjs-basic/">here</a>.</em></p>
253 <p>Suppose we want to have a simple Shiny app that collects a user’s
254 basic information (name, age, company) and submits it, along with the
255 time of submission. Here is a very simple implementation of such an app
256 (nothing actually happens when the user “submits”).</p>
257 <pre><code>library(shiny)
258 shinyApp(
259 ui = fluidPage(
260 div(id = &quot;myapp&quot;,
261 h2(&quot;shinyjs demo&quot;),
262 textInput(&quot;name&quot;, &quot;Name&quot;, &quot;&quot;),
263 numericInput(&quot;age&quot;, &quot;Age&quot;, 30),
264 textInput(&quot;company&quot;, &quot;Company&quot;, &quot;&quot;),
265 p(&quot;Timestamp: &quot;, span(date())),
266 actionButton(&quot;submit&quot;, &quot;Submit&quot;)
267 )
268 ),
269
270 server = function(input, output) {
271 }
272 )</code></pre>
273 <p><em>Note that I generally don’t like running Shiny apps like this and
274 prefer to declare the UI and server separately, but this style is used
275 here for brevity.</em></p>
276 <p>Here is what that app would look like</p>
277 <div class="float">
278 <img role="img" aria-label="Demo app" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVQAAAFUCAYAAAB7ksS1AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAACD1SURBVHhe7d1faCNnuufxx+m2oXfMQHdoZph4R+3GFzKZ6Rs7N2ERjcEcFnQ4s+slF4lZ0zdedhcdNmJnbgwnuti+2GRRIFoOrPeiIzDnInO8O4cVHBaD8Ygh5yLumzDBujDt1uAMCU26mxnPNNjp+Lxv1VvSW1Lpn+u1JFvfT6i0VFUqVZWkXz1vvSVrTERO1QAAiOkV8y8AICYCFQAcCTX5T09p/QNAL8bGdIz6qFABwBECFQAcGXCgVqW4PC/z82pYLqp7Z2EtYz4nZTN2OLjYvvM07OsHXCxUqADgCIEKAI5cgkBNyMrGruzu6iEnKTN2OFjrtrGi7gG4zKhQAcARAhUAHHEYqHZve3jI9dj1Xs41LmNZii27oNv18kf1YpclF1q2Hlosv5yrzbPcegV81aIsN83bTS+6u/0WLWL5Pffot1jHtito7efafM3LidqvTa9/l+vb/L5Rg5udCHTFTaB6wbMkhYq536CU1W/uLi5pMqGULZn7NRUpLHURah3seB+4rDQt3iy/6cOXWpC0uVnZ2mn7oa7ubKmlaElZvNvl2VJX+62FanE5evmVgiy1PUjVtVyGVsqqad0tx39tm5dTKSxZ+90P4abXv9P6mgNf8/tG8dbR1cEJaC9+oOoPiv1OTmZk0+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HRVmuVbnWtKoer6rOon6MVQF7y7Due4IKVVenS1KoiFQKSwMKdwAX2QACNSHTyYoUHjQEViqnKtWcqhe7VyocyKqpcjcz+5INBa56joMFvwLO67BWFfC2ub+ZkWRp3TTzAyrUN1SlnBRJZjZlN9fLmgDAgAJ1ZSMvaR1wQbW4XJSzdAOl8/UATqysSlpKsm3ldHrBTE0tqGnW/cS0zKjAPaDvCYBDA+qU0uctg/OnKlwrBVkKNcO7kZTp0HlPXfmK7NdSsnF6430AcGtAgWoz4RrZDAeAi6P/gRrVuaT13AxvnLcqBxWRGcpQAAPS/0BN3ZNMsiTZhvOm1eK6lCQt/mlOv/leqp0QrUpxvWRu15Wy9WAu57JSSmYkdPEAAPTRgDqldiU/Y86bmmFpa1E2a738jR1XayKrGVEZa0lKJj8t6+bx2f2MbG6sqEfGkZC7i0kumwJwJrF+U+rhw4cyNzdn7oH9AYweZ78pBQCoI1ABwBECFQAcIVABwBECFQAcIVABwBECFQAciRWo4+PjcnJyYu6NNr0f9P4AMLpiBerk5KQ8ffrU3Bttej/o/QFgdMUK1KmpKfnqq6/k66+/HtlKVW+33n69H/T+ADC6Yn31VDs+PpbDw0M5OjoayVDVzXxdmeownZiYMGMBjAr7q6exAxUARhnf5QeAc0CgAoAjBCoAOEKgAoAjBCoAOEKgAoAjocumAABnR4UKAI4QqADghMg/A7pSMeYSPslhAAAAAElFTkSuQmCC" alt="Demo app" />
279 <div class="figcaption">Demo app</div>
280 </div>
281 <div id="add-shinyjs-features" class="section level3">
282 <h3>Add shinyjs features</h3>
283 <p>Now suppose we want to add a few features to the app to make it a bit
284 more user-friendly. First we need to set up the app to use
285 <code>shinyjs</code> by making a call to <code>useShinyjs()</code> in
286 the Shiny app’s UI.</p>
287 <p>Here are 7 features we’ll add to the app, each followed with the code
288 to implement it using <code>shinyjs</code>:</p>
289 <p><strong>1. The “Name” field is mandatory and thus the “Submit” button
290 should not be enabled if there is no name</strong></p>
291 <p>In the server portion, add the following code</p>
292 <pre><code>observe({
293 if (is.null(input$name) || input$name == &quot;&quot;) {
294 shinyjs::disable(&quot;submit&quot;)
295 } else {
296 shinyjs::enable(&quot;submit&quot;)
297 }
298 })</code></pre>
299 <p>Or instead you can use the <code>toggleState</code> function and pass
300 it a <code>condition</code>:</p>
301 <pre><code>observe({
302 shinyjs::toggleState(&quot;submit&quot;, !is.null(input$name) &amp;&amp; input$name != &quot;&quot;)
303 })</code></pre>
304 <p>You can use the optional <code>condition</code> in some other
305 functions as well, which can be very useful to make your code shorter
306 and more understandable.</p>
307 <p><strong>2. The “Age” and “Company” fields are optional and we want to
308 have the ability to hide that section of the form</strong></p>
309 <p>First, we need to section off the “Age” and “Company” elements into
310 their own section, so we surround them with a <code>div</code></p>
311 <pre><code>div(id = &quot;advanced&quot;,
312 numericInput(&quot;age&quot;, &quot;Age&quot;, 30),
313 textInput(&quot;company&quot;, &quot;Company&quot;, &quot;&quot;)
314 )</code></pre>
315 <p>We also need to add a link in the UI that will be used to hide/show
316 the section</p>
317 <pre><code>a(id = &quot;toggleAdvanced&quot;, &quot;Show/hide advanced info&quot;)</code></pre>
318 <p>Lastly, we need to tell Shiny to show/hide the section when the link
319 is clicked by adding this code to the server</p>
320 <pre><code>shinyjs::onclick(&quot;toggleAdvanced&quot;,
321 shinyjs::toggle(id = &quot;advanced&quot;, anim = TRUE))</code></pre>
322 <p><strong>3. Similarly, since we don’t really care about “Age” and
323 “Company” too much, we want to hide them initially when the form
324 loads</strong></p>
325 <p>Simply surround the section we want to hide initially with
326 <code>shinyjs::hidden</code></p>
327 <pre><code>shinyjs::hidden(
328 div(id = &quot;advanced&quot;,
329 ...
330 ))</code></pre>
331 <p><strong>4. The user should be able to update the “Timestamp” in case
332 he spends way too long filling out the form (not very realistic here,
333 and the timestamp should ideally be determined when the button is
334 clicked, but it’s good enough for illustration purposes)</strong></p>
335 <p>First, we need to add an “Update” link to click on, and we need to
336 give the element showing the time an <code>id</code> so that we can
337 refer to it later when we want to change its contents.</p>
338 <p>To do that, replace <code>p(&quot;Timestamp: &quot;, span(date()))</code>
339 with</p>
340 <pre><code>p(&quot;Timestamp: &quot;, span(id = &quot;time&quot;, date()), a(id = &quot;update&quot;, &quot;Update&quot;))</code></pre>
341 <p>Now we need to tell Shiny what to do when “Update” is clicked by
342 adding this to the server</p>
343 <pre><code>shinyjs::onclick(&quot;update&quot;, shinyjs::html(&quot;time&quot;, date()))</code></pre>
344 <p><strong>5. Some users may find it hard to read the small text in the
345 app, so there should be an option to increase the font size</strong></p>
346 <p>First, we need to add checkbox to the UI</p>
347 <pre><code>checkboxInput(&quot;big&quot;, &quot;Bigger text&quot;, FALSE)</code></pre>
348 <p>In order to make the text bigger, we will use CSS. So let’s add an
349 appropriate CSS rule by adding this code to the UI</p>
350 <pre><code>shinyjs::inlineCSS(list(.big = &quot;font-size: 2em&quot;))</code></pre>
351 <p>Lastly, we want the text to be big or small depending on whether the
352 checkbox is checked by adding this code to the server</p>
353 <pre><code>observe({
354 if (input$big) {
355 shinyjs::addClass(&quot;myapp&quot;, &quot;big&quot;)
356 } else {
357 shinyjs::removeClass(&quot;myapp&quot;, &quot;big&quot;)
358 }
359 })</code></pre>
360 <p>Or, again, we can use the <code>toggleClass</code> function with the
361 <code>condition</code> argument:</p>
362 <pre><code>observe({
363 shinyjs::toggleClass(&quot;myapp&quot;, &quot;big&quot;, input$big)
364 })</code></pre>
365 <p><strong>6. Give the user a “Thank you” message upon
366 submission</strong></p>
367 <p>Simply add the following to the server</p>
368 <pre><code>observeEvent(input$submit, {
369 shinyjs::alert(&quot;Thank you!&quot;)
370 })</code></pre>
371 <p><strong>7. Allow the user to reset the form</strong></p>
372 <p>First let’s add a button to the UI</p>
373 <pre><code>actionButton(&quot;reset&quot;, &quot;Reset form&quot;)</code></pre>
374 <p>And when the button is clicked, reset the form</p>
375 <pre><code>observeEvent(input$reset, {
376 shinyjs::reset(&quot;myapp&quot;)
377 })</code></pre>
378 </div>
379 <div id="final-code" class="section level3">
380 <h3>Final code</h3>
381 <p>The final code looks like this</p>
382 <pre><code>library(shiny)
383 shinyApp(
384 ui = fluidPage(
385 shinyjs::useShinyjs(),
386 shinyjs::inlineCSS(list(.big = &quot;font-size: 2em&quot;)),
387 div(id = &quot;myapp&quot;,
388 h2(&quot;shinyjs demo&quot;),
389 checkboxInput(&quot;big&quot;, &quot;Bigger text&quot;, FALSE),
390 textInput(&quot;name&quot;, &quot;Name&quot;, &quot;&quot;),
391 a(id = &quot;toggleAdvanced&quot;, &quot;Show/hide advanced info&quot;, href = &quot;#&quot;),
392 shinyjs::hidden(
393 div(id = &quot;advanced&quot;,
394 numericInput(&quot;age&quot;, &quot;Age&quot;, 30),
395 textInput(&quot;company&quot;, &quot;Company&quot;, &quot;&quot;)
396 )
397 ),
398 p(&quot;Timestamp: &quot;,
399 span(id = &quot;time&quot;, date()),
400 a(id = &quot;update&quot;, &quot;Update&quot;, href = &quot;#&quot;)
401 ),
402 actionButton(&quot;submit&quot;, &quot;Submit&quot;),
403 actionButton(&quot;reset&quot;, &quot;Reset form&quot;)
404 )
405 ),
406
407 server = function(input, output) {
408 observe({
409 shinyjs::toggleState(&quot;submit&quot;, !is.null(input$name) &amp;&amp; input$name != &quot;&quot;)
410 })
411
412 shinyjs::onclick(&quot;toggleAdvanced&quot;,
413 shinyjs::toggle(id = &quot;advanced&quot;, anim = TRUE))
414
415 shinyjs::onclick(&quot;update&quot;, shinyjs::html(&quot;time&quot;, date()))
416
417 observe({
418 shinyjs::toggleClass(&quot;myapp&quot;, &quot;big&quot;, input$big)
419 })
420
421 observeEvent(input$submit, {
422 shinyjs::alert(&quot;Thank you!&quot;)
423 })
424
425 observeEvent(input$reset, {
426 shinyjs::reset(&quot;myapp&quot;)
427 })
428 }
429 )</code></pre>
430 <p>You can view the final app <a href="https://daattali.com/shiny/shinyjs-basic/">here</a>.</p>
431 </div>
432 </div>
433
434
435
436 <!-- code folding -->
437
438
439 <!-- dynamically load mathjax for compatibility with self-contained -->
440 <script>
441 (function () {
442 var script = document.createElement("script");
443 script.type = "text/javascript";
444 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
445 document.getElementsByTagName("head")[0].appendChild(script);
446 })();
447 </script>
448
449 </body>
450 </html>