Mastering Shiny
by Hadley Wickham
Copyright 2021 Hadley Wickham. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly 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 corporate@oreilly.com .
- 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
Revision History for the First Edition
- 2020-04-29: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492047384 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Mastering Shiny, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers 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 Attribution-NonCommercial-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 youve 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 dont 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 youre starting to have problems managing the complexity.
What Will You Learn?
The book is divided into four parts:
In , youll learn the basics of Shiny so you can get up and running as quickly as possible. Youll learn about the basics of app structure, useful UI components, and the foundations of reactive programming.
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 , youll go deep into the theory and practice of reactive programming, the programming paradigm that underlies Shiny. If youre an existing Shiny user, youll 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 well finish up with a survey of useful techniques for making your Shiny apps work well in production. Youll 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 its correct, and how to measure and improve performance.
What Wont You Learn?
The focus of this book is making effective Shiny apps and understanding the underlying theory of reactivity. Ill do my best to showcase best practices for data science, R programming, and software engineering, but youll 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 dont cover:
This book only covers the built-in user interface toolkit. This doesnt provide the sexiest possible design, but its 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 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 youre new to Shiny in production, I recommend starting with Joe Chengs 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 youre likely to face. Once youve done that, see the RStudio Connect website to learn about RStudios product for deploying apps within your company, and the Shiny website for other common deployment scenarios .