A Gentle Introduction to R Shiny
Written by Matt Dancho on March 16, 2021
This article is part of a R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks.
What if you could turn your #datascience analysis into a web application? You can do EXACTLY that with
R Shiny is an amazing framework built to convert your data analysis into a web app - FAST! Create amazing applications your business can use in hours (not months!).
Here are the links to get set up. 👇
Shiny Explorer App
In this R-Tip, you create an AWESOME Correlation Plot Heatmap that can be used for fast Exploratory Data Analysis (EDA). This application uses:
- Shiny Inputs to change the connection to the dataset (3 Options Available: StackOverflow, Car Prices, Sacramento Housing).
- DataExplorer to create a Correlation Heatmap & Plotly to make the heatmap interactive.
- Shiny and BSLib to create awesome bootstrap 4 theme "Minty"
The Shiny App with Correlation Heat Map
Why use R Shiny?
I get this question a ton: Why R Shiny vs Tableau or PowerBI or any other "dashboarding" tool?
R Shiny is much more than a dashboarding tool.
I love showing off this app. Nostradamus is an example of the high-end of what you can accomplish when you learn R Shiny and time series forecasting with Modeltime. Nostradamus is an Auto-Forecasting App that makes 28 machine learning models on the fly and combines the best into an ensemble. The forecasting approach adjusts automatically. Internally the app uses:
Modeltimefor Time Series Forecasting and Ensembling.
TimeTkfor Time Series Data Wrangling, Visualizations, and Feature Engineering.
Shinyfor packaging the analysis in a User-Friendly Way
Learning Shiny for Your Career
The key differences between Shiny and Tableau are important to understand as they relate to the concept of the Full-Stack Data Scientist (a data scientist that can make and distribute web applications powered with data science). The bottom line is that R Shiny allows businesses to truly scale decisions beyond a simple dashboard.
This is why I highly recommend learning R Shiny for your career. Businesses need apps that can forecast sales demand, predict customer churn, and distribute actionable insights real-time. Shiny does all of this.
But first thing first - Let's create your First Shiny App!
Creating Your First Shiny App
R Shiny can be intimidating. If this is your first time building an app, I strongly recommend watching the YouTube Video - Gentle Intro to R Shiny Apps (11 min). It will help immensely.
As you go through this tutorial, I've added the "Shinyverse" to my Ultimate R Cheatsheet (see page 2). The most important part is at the top, which includes links the the key R packages that make up an expanding ecosystem of shiny R packages. 👇
How does R Shiny work?
At it's core, R Shiny is a web framework that combines a User Interface (controls app layout and appearance) with a Server (runs R, controls app functionality).
The User Interface (UI)
Think of the UI as the scaffolding and theming elements that position your app's output and make it look amazing!
We can add shiny inputs to an app. These are the elements that your user interacts with. They tell your server (discussed next) when something is happening.
When we run the app, the
shiny selectInput() generates this dropdown in our UI.
The Server (R Code is Run Here)
Think of the Server as where your R Code runs when the user interacts with your app.
Reactivity & Observers
What we are doing: We store reactive values and modify them inside of observers.
What this means:
- We are creating a way to watch our users interactions with the apps.
- When the user changes the dropdown selection from "Stack Overflow" to "Car Prices", the data sets will change.
Then, any functions downstream that use the
rv$data_set will fire, updating the data accordingly.
Finally, our app's UI then updates because we have an
outputPlotly() in our UI that references the "corrplot" on our server.
Now, when we use the Shiny App dropdowns, our app fires and the datasets change on the fly!
You just built your first Shiny App! Congratulations.
You should be proud.
But, what if you want to build more powerful applications?
This could be a challenge. You'll need to learn a ton, and this will take a long time. Plus, you might struggle and quit.
The number one reason that people quit: They get an error that they can't figure out.
Errors stop your progress... Grinding to a halt.
What if there was a program that took the guess-work out of learning data science and made it impossible to fail?
👇 Top R-Tips Tutorials you might like:
- mmtable2: ggplot2 for tables
- ggdist: Make a Raincloud Plot to Visualize Distribution in ggplot2
- ggside: Plot linear regression with marginal distributions
- DataEditR: Interactive Data Editing in R
- openxlsx: How to Automate Excel in R
- officer: How to Automate PowerPoint in R
- DataExplorer: Fast EDA in R
- esquisse: Interactive ggplot2 builder
- gghalves: Half-plots with ggplot2
- rmarkdown: How to Automate PDF Reporting
- patchwork: How to combine multiple ggplots
- Geospatial Map Visualizations in R
Want these tips every week? Join R-Tips Weekly.