1,293 163 8MB
English Pages 523 Year 2021
Mastering Shiny Build Interactive Apps, Reports, and Dashboards Powered by R Hadley Wickham
Mastering Shiny by Hadley Wickham Copyright © 2021 Hadley Wickham. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Acquisitions Editor: Jessica Haberman Development Editor: Melissa Potter Production Editor: Christopher Faucher Copyeditor: nSight, Inc. Proofreader: Piper Editorial Consulting, LLC Indexer: Judith McConville Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Kate Dullea May 2021: First Edition Revision History for the First Edition
2020-04-29: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492047384 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Mastering Shiny, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author, and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. Mastering Shiny is available under the Creative Commons AttributionNonCommercial-NoDerivatives 4.0 International License. The author maintains an online version at https://mastering-shiny.org. 978-1-492-04738-4 [LSI]
Preface What Is Shiny? 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: You need a deep knowledge of web technologies like HTML, CSS, and JavaScript. 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: Providing a carefully curated set of user interface (UI for short) functions that generate the HTML, CSS, and JavaScript needed for common tasks. This means that you don’t need to know the details of HTML/CSS/JavaScript until you want to go beyond the basics that Shiny provides for you. 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 nontechnical audience with informative visualizations 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.
Who Should Read This Book? 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.
What Will You Learn? The book is divided into four parts: 1. In Part I, 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. 2. Part II 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. 3. In Part III, 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. 4. Finally, in Part IV 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 organize your code, how to test your code to ensure it’s correct, and how to measure and improve performance.
What Won’t You Learn? 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 a number of other packages that provide alternative frontends. See “Bootstrap” 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 organizational, not technical). If you’re new to Shiny in production, I recommend starting with Joe Cheng’s 2019 rstudio::conf keynote. 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.
Prerequisites 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 downloading RStudio Desktop and giving it a try, but it’s not required to be successful with Shiny or with this book. 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.
Conventions Used in This Book The following typographical conventions are used in this book: Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by values determined by context. TIP This element signifies a tip or suggestion.
NOTE This element signifies a general note.
Using Code Examples Supplemental material (code examples, exercises, etc.) is available for download at https://mastering-shiny.org. The code samples in this book are licensed under the MIT License. If you have a technical question or a problem using the code examples, please send email to [email protected].
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but generally do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Mastering Shiny by Hadley Wickham (O’Reilly). Copyright 2021 Hadley Wickham, 978-1-492-04738-4.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at [email protected].
O’Reilly Online Learning NOTE For more than 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed.
Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, visit http://oreilly.com.
How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at https://oreil.ly/mastering-shiny. Email [email protected] to comment or ask technical questions about this book. For news and information about our books and courses, visit http://oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://youtube.com/oreillymedia
Acknowledgments 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-mffm), 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 (@joachimgassen), 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), and gXcloud (@xwydq).
How This Book Was Built This book was written in RStudio using bookdown. The book’s website is hosted with netlify and is automatically updated after every commit by GitHub Actions. The complete source is available from GitHub. This version of the book was built with R version 4.0.3 (2020-10-10) and the following packages:
Package
Version
Source
gapminder
0.3.0
standard (@0.3.0)
ggforce
0.3.2
standard (@0.3.2)
gh
1.2.0
standard (@1.2.0)
globals
0.14.0
standard (@0.14.0)
openintro
2.0.0
standard (@2.0.0)
profvis
0.3.7.9000
GitHub (rstudio/profvis@ca1b272)
RSQLite
2.2.3
standard (@2.2.3)
shiny
1.6.0
standard (@1.6.0)
shinycssloaders
1.0.0
standard (@1.0.0)
shinyFeedback
0.3.0
standard (@0.3.0)
shinythemes
1.2.0
standard (@1.2.0)
testthat
3.0.2.9000
GitHub (r-lib/testthat@4793514)
thematic
0.1.1
GitHub (rstudio/thematic@d78d24a)
tidyverse
1.3.0
standard (@1.3.0)
vroom
1.3.2
standard (@1.3.2)
waiter
0.2.0
standard (@0.2.0)
xml2
1.3.2
standard (@1.3.2)
zeallot
0.1.0
standard (@0.1.0)
Part I. Getting Started The goal of the next four chapters is to get you writing Shiny apps as quickly as possible. In Chapter 1, I’ll start small, but complete, showing you all the major pieces of an app and how they fit together. Then in Chapters 2 and 3 you’ll start to get into the details of the two major parts of a Shiny app: the frontend (what the user sees in the browser) and the backend (the code that makes it all work). We’ll finish up in Chapter 4 with a case study to help cement the concepts you’ve learned so far.
Chapter 1. Your First Shiny App Introduction In this chapter, we’ll create a simple Shiny app. I’ll start by showing you the minimum boilerplate needed for a Shiny app, and then you’ll learn how to start and stop it. Next you’ll learn the two key components of every Shiny app: the UI (short for user interface), which defines how your app looks, and the server function, which defines how your app works. Shiny uses reactive programming to automatically update outputs when inputs change, so we’ll finish off the chapter by learning the third important component of Shiny apps: reactive expressions. If you haven’t already installed Shiny, install it now with: install.packages("shiny")
If you’ve already installed Shiny, use packageVersion("shiny") to check that you have version 1.5.0 or greater. Then load in your current R session: library(shiny)
Create App Directory and File There are several ways to create a Shiny app. The simplest is to create a new directory for your app and put a single file called app.R in it. This app.R file will be used to tell Shiny both how your app should look and how it should behave. Try it out by creating a new directory and adding an app.R file that looks like this:
library(shiny) ui