A Gentle Introduction to R Shiny
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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
.
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. ????
![](https://i0.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/Video.png?w=578&ssl=1)
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:
Modeltime
for Time Series Forecasting and Ensembling.TimeTk
for Time Series Data Wrangling, Visualizations, and Feature Engineering.Shiny
for 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.
PRO TIP
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. ????
![](https://i1.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/shinyverse.jpg?w=450&ssl=1)
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!
![](https://i1.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/user_interface.jpg?w=450&ssl=1)
Shiny Inputs
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.
![](https://i2.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/shiny_input.jpg?w=450&ssl=1)
When we run the app, the shiny selectInput()
generates this dropdown in our UI.
![](https://i0.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/stack_overflow.jpg?w=450&ssl=1)
The Server (R Code is Run Here)
Think of the Server as where your R Code runs when the user interacts with your app.
![](https://i2.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/server.jpg?w=450&ssl=1)
Reactivity & Observers
Inside the server, R Shiny uses a concept called “Reactivity”. I teach this concept in-depth using many examples in my Shiny Dashboard Course and Shiny Developer with AWS Course.
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.
![](https://i0.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/reactivity_observers.jpg?w=450&ssl=1)
Then, any functions downstream that use the rv$data_set
will fire, updating the data accordingly.
![](https://i2.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/reactivity.jpg?w=450&ssl=1)
Finally, our app’s UI then updates because we have an outputPlotly()
in our UI that references the “corrplot” on our server.
![](https://i0.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/rendering_output.jpg?w=450&ssl=1)
Now, when we use the Shiny App dropdowns, our app fires and the datasets change on the fly!
![](https://i0.wp.com/www.business-science.io/assets/2021-03-16-Intro-R-Shiny/dataset_change.gif?w=450&ssl=1)
In Summary
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:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.