
This document contains a collection of various Shiny tricks that I commonly use or that I know many people ask about. Each link contains a complete functional Shiny app that demonstrates how to perform a non trivial task in Shiny.
Since I first learned about Shiny 2 years ago, I was always looking for ways to push Shiny to its limits and I enjoyed finding ways to work around common problems people were having (the harder the problem, the better!). I’ve built many Shiny apps over these 2 years, both for myself and as a contractor for other people/companies, and throughout this time I developed a handy list of Shiny design patterns and tricks, some of which I present here.
Prereq: How to hide/show something in Shiny? How to disable an input? How do I reset an input?
A few very common questions in Shiny are “how do I hide/show something”, “how do I disable an input”, and “how do I reset an input”. Many of the code samples in this document also rely on being able to do these things, so I wanted to start by saying that I will be using the shinyjs package to do all that. (Yes, I know it looks like I’m shamelessly advertising shinyjs by saying this… but it is going to be useful for many concepts here)
Show a spinning "loading" animation while a plot is recalculating
When a Shiny plot is recalculating, the plot gets grayed out. This app shows how you can add a spinner wheel on top of the plot while it is recalculating, to make it clear to the user that the plot is reloading. There can be many different ways to achieve a similar result using different combinations of HTML/CSS, this example is just the simplest one I came up with.
Hide a tab
This app demonstrates how shinyjs can be used to hide/show a specific tab in a tabsetPanel. In order to use this trick, the tabsetPanel must have an id. Using this id and the value of the specific tab you want to hide/show, you can call shinyjs::hide/shinyjs::show/shinyjs::toggle.
A common question regarding shinydashboard is how to programmatically hide/show the sidebar. This can very easily be done using the shinyjs package, as demonstrated here.
Loading screen
This simple app shows how to add a “Loading…” screen overlaying the main app while the app’s server is initializing. The main idea is to include an overlay element that covers the entire app (using CSS), hide the main app’s UI, and at the end of the server function show the UI and hide the overlay.
Automatically stop a Shiny app when closing the browser tab
When developing a Shiny app and running the app in the browser (as opposed to inside the RStudio Viewer), it can be annoying that when you close the browser window, the app is still running and you need to manually press “Esc” to kill it. By adding a single line to the server code session$onSessionEnded(stopApp), a Shiny app will automatically stop running whenever the browser tab (or any session) is closed.
Close the window (and stop the app) with a button click
This simple example shows how you can have a button that, when clicked, will close the current browser tab and stop the running Shiny app (you can choose to do only one of these two actions).
Select input with more breathing room
One common CSS question in Shiny is how to make the select input dropdown menu have some more whitespace. It’s actually very easy to do with just two CSS rules, as demonstrated in this example.
Select input with groupings of options
This isn’t really a trick as much as an undocumented feature in Shiny that not many people know about. Usually when people write dropdowns in Shiny, all the options are just provided as one long list. But it is possible to have groups of items, and it’s very easy to do.
Getting the value of an object in a running Shiny app without access to a debugger
Sometimes you may need to know the value of some variable/function call in a Shiny app when you don’t have easy access to debugging tools. For example, suppose you deploy your shiny app on shinyapps.io and it’s running into a weird error there. You’re sure that it’s because one of the packages on the shinyapps.io server is not the version that you expect, but you want to make sure that your suspicion is correct. It’s a bit difficult to debug on shinyapps.io (one thing you could do is try to use the log files), but there’s a quick and easy way to see any value in a Shiny app in real-time.