| 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>Including shinyjs in different types of apps</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">Including shinyjs in different types of |
| 241 |
apps</h1> |
| 242 |
<h4 class="author">Dean Attali</h4> |
| 243 |
<h4 class="date">2026-01-14</h4> |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
<div id="including-shinyjs-in-different-types-of-apps" class="section level1"> |
| 248 |
<h1>Including shinyjs in different types of apps</h1> |
| 249 |
<div id="table-of-contents" class="section level2"> |
| 250 |
<h2>Table of contents</h2> |
| 251 |
<ul> |
| 252 |
<li><a href="#usage-basic">Basic use of shinyjs</a></li> |
| 253 |
<li><a href="#usage-dashboard">Using shinyjs in Shiny |
| 254 |
Dashboards</a></li> |
| 255 |
<li><a href="#usage-navbarpage">Using shinyjs with navbarPage |
| 256 |
layout</a></li> |
| 257 |
<li><a href="#usage-rmd">Using shinyjs in R Markdown documents</a> |
| 258 |
<ul> |
| 259 |
<li><a href="#usage-tabbed">Rmd documents with Tabbed Sections</a></li> |
| 260 |
<li><a href="#usage-prerendered">Rmd documents using |
| 261 |
<code>shiny_prerendered</code> engine</a></li> |
| 262 |
</ul></li> |
| 263 |
<li><a href="#usage-html">Using shinyjs when the user interface is built |
| 264 |
using an HTML file</a></li> |
| 265 |
</ul> |
| 266 |
<h2 id="usage-basic"> |
| 267 |
Basic use of shinyjs |
| 268 |
</h2> |
| 269 |
<p>A typical Shiny app has a UI portion and a server portion. Before |
| 270 |
using most shinyjs functions, you need to call <code>useShinyjs()</code> |
| 271 |
in the app’s UI. It’s best to include it near the top as a |
| 272 |
convention.</p> |
| 273 |
<p>Here is a minimal Shiny app that uses <code>shinyjs</code>:</p> |
| 274 |
<pre><code>library(shiny) |
| 275 |
library(shinyjs) |
| 276 |
|
| 277 |
ui <- fluidPage( |
| 278 |
useShinyjs(), # Include shinyjs |
| 279 |
|
| 280 |
actionButton("button", "Click me"), |
| 281 |
textInput("text", "Text") |
| 282 |
) |
| 283 |
|
| 284 |
server <- function(input, output) { |
| 285 |
observeEvent(input$button, { |
| 286 |
toggle("text") # toggle is a shinyjs function |
| 287 |
}) |
| 288 |
} |
| 289 |
|
| 290 |
shinyApp(ui, server)</code></pre> |
| 291 |
<p>This is how most Shiny apps should initialize <code>shinyjs</code> - |
| 292 |
by calling <code>useShinyjs()</code> near the top of the UI.</p> |
| 293 |
<p>However, if you use shinyjs in any of the following cases:</p> |
| 294 |
<ul> |
| 295 |
<li>In Shiny dashboards (built using the <code>shinydashboard</code> |
| 296 |
package)</li> |
| 297 |
<li>In Shiny apps that use a <code>navbarPage</code> layout</li> |
| 298 |
<li>In Rmd documents</li> |
| 299 |
<li>In Shiny apps that manually build the user interface with an HTML |
| 300 |
file or template (instead of using Shiny’s UI functions)</li> |
| 301 |
</ul> |
| 302 |
<p>Then the following sections will show you how you to include |
| 303 |
shinyjs.</p> |
| 304 |
<h2 id="usage-dashboard"> |
| 305 |
Using shinyjs in Shiny Dashboards |
| 306 |
</h2> |
| 307 |
<p><code>shinydashboard</code> is an R package that lets you create nice |
| 308 |
dashboards with Shiny. Since it has a different structure than typical |
| 309 |
Shiny apps, it can be unclear where to include the call to |
| 310 |
<code>useShinyjs()</code> in these apps. It is recommended to place the |
| 311 |
call to <code>useShinyjs()</code> in the beginning of |
| 312 |
<code>dashboardBody()</code>. For example, here is a minimal Shiny |
| 313 |
dashboard that uses <code>shinyjs</code>:</p> |
| 314 |
<pre><code>library(shiny) |
| 315 |
library(shinydashboard) |
| 316 |
library(shinyjs) |
| 317 |
|
| 318 |
ui <- dashboardPage( |
| 319 |
dashboardHeader(), |
| 320 |
dashboardSidebar(), |
| 321 |
dashboardBody( |
| 322 |
useShinyjs(), |
| 323 |
actionButton("button", "Click me"), |
| 324 |
div(id = "hello", "Hello!") |
| 325 |
) |
| 326 |
) |
| 327 |
|
| 328 |
server <- function(input, output) { |
| 329 |
observeEvent(input$button, { |
| 330 |
toggle("hello") |
| 331 |
}) |
| 332 |
} |
| 333 |
|
| 334 |
shinyApp(ui, server)</code></pre> |
| 335 |
<h2 id="usage-navbarpage"> |
| 336 |
Using shinyjs with navbarPage layout |
| 337 |
</h2> |
| 338 |
<p>When creating a Shiny app that uses a <code>navbarPage</code> layout, |
| 339 |
the call to <code>useShinyjs()</code> can be placed inside any of the |
| 340 |
tabs (since the only real requirement is that it will be present |
| 341 |
<em>somewhere</em> in the UI). While having <code>useShinyjs()</code> |
| 342 |
inside the contents of any tab will work, there is another method that |
| 343 |
is preferred. You can wrap the <code>navbarPage</code> in a |
| 344 |
<code>tagList</code>, and call <code>useShinyjs()</code> within the |
| 345 |
<code>tagList</code>. This way, <code>shinyjs</code> gets set up in a |
| 346 |
way that is independent of each of the tabs. For example, here is a |
| 347 |
minimal Shiny app that uses <code>shinyjs</code> inside a |
| 348 |
<code>navbarPage</code> layout:</p> |
| 349 |
<pre><code>library(shiny) |
| 350 |
library(shinyjs) |
| 351 |
|
| 352 |
ui <- tagList( |
| 353 |
useShinyjs(), |
| 354 |
navbarPage( |
| 355 |
"shinyjs with navbarPage", |
| 356 |
tabPanel("tab1", |
| 357 |
actionButton("button", "Click me"), |
| 358 |
div(id = "hello", "Hello!")), |
| 359 |
tabPanel("tab2") |
| 360 |
) |
| 361 |
) |
| 362 |
|
| 363 |
server <- function(input, output, session) { |
| 364 |
observeEvent(input$button, { |
| 365 |
toggle("hello") |
| 366 |
}) |
| 367 |
} |
| 368 |
|
| 369 |
shinyApp(ui, server)</code></pre> |
| 370 |
<h2 id="usage-rmd"> |
| 371 |
Using shinyjs in R Markdown documents |
| 372 |
</h2> |
| 373 |
<p>It is possible to embed Shiny components in an R Markdown document, |
| 374 |
resulting in interactive R Markdown documents. More information on how |
| 375 |
to use these documents is available <a href="https://bookdown.org/yihui/rmarkdown/shiny-documents.html">on the |
| 376 |
R Markdown website</a>. Even though interactive documents don’t |
| 377 |
explicitly specify a UI and a server, using <code>shinyjs</code> is |
| 378 |
still easy: simply call <code>useShinyjs(rmd = TRUE)</code> (note the |
| 379 |
<code>rmd = TRUE</code> argument). For example, the following code can |
| 380 |
be used inside an R Markdown code chunk (assuming the Rmd document is |
| 381 |
set up with <code>runtime: shiny</code> as the link above |
| 382 |
describes):</p> |
| 383 |
<pre><code>library(shinyjs) |
| 384 |
|
| 385 |
useShinyjs(rmd = TRUE) |
| 386 |
actionButton("button", "Click me") |
| 387 |
div(id = "hello", "Hello!") |
| 388 |
|
| 389 |
observeEvent(input$button, { |
| 390 |
toggle("hello") |
| 391 |
})</code></pre> |
| 392 |
<h3 id="usage-tabbed"> |
| 393 |
Rmd documents with Tabbed Sections |
| 394 |
</h3> |
| 395 |
<p>If the Rmd file makes use of <a href="https://bookdown.org/yihui/rmarkdown/html-document.html#tabbed_sections">Tabbed |
| 396 |
Sections</a> (using <code>{.tabset}</code>), then you should include the |
| 397 |
call to <code>useShinyjs(rmd = TRUE)</code> before the tabset |
| 398 |
definition, near the beginning of the file.</p> |
| 399 |
<h3 id="usage-prerendered"> |
| 400 |
Rmd documents using <code>shiny_prerendered</code> engine |
| 401 |
</h3> |
| 402 |
<p>If you’re using the <a href="https://bookdown.org/yihui/rmarkdown/shiny-documents.html"><code>shiny_prerendered</code> |
| 403 |
Rmd format</a>, you need to include the following code in the beginning |
| 404 |
of your Rmd file, just after the YAML header (you need to remove the |
| 405 |
spaces between the backticks to make this code work):</p> |
| 406 |
<pre><code>```{r, echo=FALSE} |
| 407 |
shiny::addResourcePath("shinyjs", system.file("srcjs", package = "shinyjs")) |
| 408 |
``` |
| 409 |
```{r, context="server"} |
| 410 |
shinyjs::useShinyjs(html = TRUE) |
| 411 |
``` |
| 412 |
<script src="shinyjs/inject.js"></script></code></pre> |
| 413 |
<h2 id="usage-html"> |
| 414 |
Using shinyjs when the user interface is built using an HTML |
| 415 |
file/template |
| 416 |
</h2> |
| 417 |
<p>While most Shiny apps use Shiny’s functions to build a user interface |
| 418 |
to the app, it is possible to build the UI with an HTML template, <a href="https://shiny.rstudio.com/articles/templates.html">as RStudio |
| 419 |
shows in this article</a>. In this case, you simply need to add |
| 420 |
<code>{{ useShinyjs() }}</code> somewhere in the template, preferably |
| 421 |
inside the <code><head>...</head></code> tags.</p> |
| 422 |
<p>A similar way to create your app’s UI with HTML is to write it |
| 423 |
entirely in HTML (without templates), <a href="https://shiny.rstudio.com/articles/html-ui.html">as RStudio shows |
| 424 |
in this article</a>. Building Shiny apps like this is much more |
| 425 |
complicated and should only be used if you’re very comfortable with |
| 426 |
HTML. Using <code>shinyjs</code> in these apps is possible but it works |
| 427 |
a little differently since there is no <code>ui.R</code> to call |
| 428 |
<code>useShinyjs()</code> from. There are three simple steps to take in |
| 429 |
order to use <code>shinyjs</code> in these apps:</p> |
| 430 |
<ul> |
| 431 |
<li><p>Create a <code>global.R</code> file in the same directory as your |
| 432 |
<code>server.R</code>, and add the following line to the file:</p> |
| 433 |
<pre><code> shiny::addResourcePath("shinyjs", system.file("srcjs", package = "shinyjs"))</code></pre></li> |
| 434 |
<li><p>In the <code>index.html</code> file you need to load a special |
| 435 |
JavaScript file named <code>shinyjs/inject.js</code>. You do this by |
| 436 |
adding the following line to the HTML’s <code><head></code> |
| 437 |
tag:</p> |
| 438 |
<pre><code> `<script src="shinyjs/inject.js"></script>`</code></pre></li> |
| 439 |
<li><p>In your server function (the <code>shinyServer</code> function) |
| 440 |
you need to call <code>useShinyjs(html = TRUE)</code></p></li> |
| 441 |
</ul> |
| 442 |
<p>After adding these three lines to your code, you can use all |
| 443 |
<code>shinyjs</code> functions as usual.</p> |
| 444 |
</div> |
| 445 |
</div> |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
<!-- code folding --> |
| 450 |
|
| 451 |
|
| 452 |
<!-- dynamically load mathjax for compatibility with self-contained --> |
| 453 |
<script> |
| 454 |
(function () { |
| 455 |
var script = document.createElement("script"); |
| 456 |
script.type = "text/javascript"; |
| 457 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 458 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 459 |
})(); |
| 460 |
</script> |
| 461 |
|
| 462 |
</body> |
| 463 |
</html> |