If you’ve never used Shiny before, welcome! Shiny is an R package that allows you to easily create rich, interactive web apps. Shiny allows you to take your work in R and expose it via a web browser so that anyone can use it. Shiny makes you look awesome by making it easy to produce polished web apps with a minimum amount of pain.
In the past, creating web apps was hard for most R users because:
Making complex interactive apps requires careful analysis of interaction flows to make sure that when an input changes, only the related outputs are updated.
Shiny makes it significantly easier for the R programmer to create web apps by:
Introducing a new style of programming called reactive programming which automatically tracks the dependencies of pieces of code. This means that whenever an input changes, Shiny can automatically figure out how to do the smallest amount of work to update all the related outputs.
People use Shiny to:
Create dashboards that track important high-level performance indicators, while facilitating drill down into metrics that need more investigation.
Replace hundreds of pages of PDFs with interactive apps that allow the user to jump to the exact slice of the results that they care about.
Communicate complex models to a non-technical audience with informative visualisations and interactive sensitivity analysis.
Provide self-service data analysis for common workflows, replacing email requests with a Shiny app that allows people to upload their own data and perform standard analyses. You can make sophisticated R analyses available to users with no programming skills.
Create interactive demos for teaching statistics and data science concepts that allow learners to tweak inputs and observe the downstream effects of those changes in an analysis.
In short, Shiny gives you the ability to pass on some of your R superpowers to anyone who can use the web.
This book is aimed at two main audiences:
R users who are interested in learning about Shiny in order to turn their analyses into interactive web apps. To get the most out of this book, you should be comfortable using R to do data analysis, and should have written at least a few functions.
Existing Shiny users who want to improve their knowledge of the theory underlying Shiny in order to write higher-quality apps faster and more easily. You should find this book particularly helpful if your apps are starting to get bigger and you’re starting to have problems managing the complexity.
The book is divided into four parts:
In “Getting started”, you’ll learn the basics of Shiny so you can get up and running as quickly as possible. You’ll learn about the basics of app structure, useful UI components, and the foundations of reactive programming.
“Shiny in action” builds on the basics to help you solve common problems including giving feedback to the user, uploading and downloading data, generating UI with code, reducing code duplication, and using Shiny to program the tidyverse.
In “Mastering reactivity”, you’ll go deep into the theory and practice of reactive programming, the programming paradigm that underlies Shiny. If you’re an existing Shiny user, you’ll get the most value out of this chapter as it will give you a solid theoretical underpinning that will allow you to create new tools specifically tailored for your problems.
Finally, in “Best practices” we’ll finish up with a survey of useful techniques for making your Shiny apps work well in production. You’ll learn how to decompose complex apps into functions and modules, how to use packages to organise your code, how to test your code to ensure it’s correct, and how to measure and improve performance.
The focus of this book is making effective Shiny apps and understanding the underlying theory of reactivity. I’ll do my best to showcase best practices for data science, R programming, and software engineering, but you’ll need other references to master these important skills. If you enjoy my writing in this book, you might enjoy my other books on these topics: R for Data Science, Advanced R, and R Packages.
There are also a number of important topics specific to Shiny that I don’t cover:
This book only covers the built-in user interface toolkit. This doesn’t provide the sexiest possible design, but it’s simple to learn and gets you a long way. If you have additional needs (or just get bored with the defaults), there are number of other packages that provide alternative front ends. See Section ?? for more details.
Deployment of Shiny apps. Putting Shiny “into production” is outside the scope of this book because it hugely varies from company to company, and much of it is unrelated to R (the majority of challenges tend to be cultural or organisational, not technical). If you’re new to Shiny in production, I recommend by starting with Joe Cheng’s 2019 rstudio::conf keynote: https://rstudio.com/resources/rstudioconf-2019/shiny-in-production-principles-practices-and-tools/. That will give you the lay of the land, discussing broadly what putting Shiny into production entails and how to overcome some of the challenges that you’re likely to face. Once you’ve done that, see the RStudio Connect website to learn about RStudio’s product for deploying apps within your company and the Shiny website for other common deployment scenarios.
Before we continue, make sure you have all the software you need for this book:
R: If you don’t have R installed already, you may be reading the wrong book; I assume a basic familiarity with R throughout this book. If you’d like to learn how to use R, I’d recommend my R for Data Science which is designed to get you up and running with R with a minimum of fuss.
RStudio: RStudio is a free and open source integrated development environment (IDE) for R. While you can write and use Shiny apps with any R environment (including R GUI and ESS), RStudio has some nice features specifically for authoring, debugging, and deploying Shiny apps. We recommend giving it a try, but it’s not required to be successful with Shiny or with this book. You can download RStudio Desktop from https://www.rstudio.com/products/rstudio/download
R packages: This book uses a bunch of R packages. You can install them all at once by running:
install.packages(c( "gapminder", "ggforce", "gh", "globals", "openintro", "profvis", "RSQLite", "shiny", "shinycssloaders", "shinyFeedback", "shinythemes", "testthat", "thematic", "tidyverse", "vroom", "waiter", "xml2", "zeallot" ))
If you’ve downloaded Shiny in the past, make sure that you have at least version 1.6.0.
This book was written in the open and chapters were advertised on twitter when complete. It is truly a community effort: many people read drafts, fixed typos, suggested improvements, and contributed content. Without those contributors, the book wouldn’t be nearly as good as it is, and I’m deeply grateful for their help.
A big thank you to all 83 people who contributed specific improvements via GitHub pull requests (in alphabetical order by username): Adam Pearce (@1wheel), Adi Sarid (@adisarid), Alexandros Melemenidis (@alex-m-ffm), Anton Klåvus (@antonvsdata), Betsy Rosalen (@betsyrosalen), Michael Beigelmacher (@brooklynbagel), Bryan Smith (@BSCowboy), c1au6io_hh (@c1au6i0), @canovasjm, Chris Beeley (@ChrisBeeley), @chsafouane, Chuliang Xiao (@ChuliangXiao), Conor Neilson (@condwanaland), @d-edison, Dean Attali (@daattali), DanielDavid521 (@Danieldavid521), David Granjon (@DivadNojnarg), Eduardo Vásquez (@edovtp), Emil Hvitfeldt (@EmilHvitfeldt), Emilio (@emilopezcano), Emily Riederer (@emilyriederer), Eric Simms (@esimms999), Federico Marini (@federicomarini), Frederik Kok Hansen (@fkoh111), Frans van Dunné (@FvD), Giorgio Comai (@giocomai), Hedley (@heds1), Henning (@henningsway), Hlynur (@hlynurhallgrims), @hsm207, @jacobxk, James Pooley (@jamespooley), Joe Cheng (@jcheng5), Julien Colomb (@jcolomb), Juan C Rodriguez (@jcrodriguez1989), Jennifer (Jenny) Bryan (@jennybc), Jim Hester (@jimhester), Joachim Gassen (@joachim-gassen), Jon Calder (@jonmcalder), Jonathan Carroll (@jonocarroll), Julian Stanley (@julianstanley), @jyuu, @kaanpekel, Karandeep Singh (@kdpsingh), Robert Kirk DeLisle (@KirkDCO), Elaine (@loomalaine), Malcolm Barrett (@malcolmbarrett), Marly Gotti (@marlycormar), Matthew Wilson (@MattW-Geospatial), Matthew T. Warkentin (@mattwarkentin), Mauro Lepore (@maurolepore), Maximilian Rohde (@maxdrohde), Matthew Berginski (@mbergins), Michael Dewar (@michael-dewar), Mine Cetinkaya-Rundel (@mine-cetinkaya-rundel), Maria Paula Caldas (@mpaulacaldas), nthobservation (@nthobservation), Pietro Monticone (@pitmonticone), psychometrician (@psychometrician), Ram Thapa (@raamthapa), Janko Thyson (@rappster), Rebecca Janis (@rbjanis), Tom Palmer (@remlapmot), Russ Hyde (@russHyde), Barret Schloerke (@schloerke), Scott (@scottyd22), Matthew Sedaghatfar (@sedaghatfar), Shixiang Wang (@ShixiangWang), Praer (Suthira Owlarn) (@sowla), Sébastien Rochette (@statnmap), @stevensbr, André Calero Valdez (@Sumidu), Tanner Stauss (@tmstauss), Tony Fujs (@tonyfujs), Stefan Moog (@trekonom), Jeff Allen (@trestletech), Trey Gilliland (@treygilliland), Albrecht (@Tungurahua), Valeri Voev (@ValeriVoev), Vickus (@Vickusr), William Doane (@WilDoane), 黄湘云 (@XiangyunHuang), gXcloud (@xwydq).
This version of the book was built with R version 4.1.1 (2021-08-10) and the following packages: