Dashboarding with R Shiny
R Shiny is a flexible development platform and dashboarding is a common use case. Packages including shinydashboard, shinydashboardPlus and others help 'specialise' R Shiny for dashboarding - however unlike major specialist dashboarding platforms like Power BI and Tableau, the dominant use case is coding over a mouse driven drag-n-drop interface. One of the many benefits of using code is the ability to use version control with git and share easily - here's the github repository with all the code. If you're new to RStudio - follow the steps below to run it on your own computer.
Let’s make it real
Common to both we agreed on the same start and end point.
Start point - a publicly available dataset on Kaggle (video game sales)
End point - a dashboard page with 2 sliders, 11 KPI, a single table and a single chart
We took two completely different journeys. In this article, I'll go into steps used to create a dashboard with R Shiny, for a comparison with Power BI there are two companion articles:
Dashboarding with Power BI vs Shiny
Dashboarding with Power BI - steps for creating a Power BI dashboard
Vlad’s journey - R Shiny
The steps I took in building the shiny dashboard included
Set up a git repository for version control (used github)
Layout the file system for the app (directories and files)
Choose R packages
Import and process data with reactives
Deploy (used shinyapps.io with free account)
While R Shiny is popular for creating dashboards, it can be used to create almost any type of web application. If one had the time and determination, one could even create a version of Power BI or Tableau using R Shiny and R packages - it’s that flexible. Here we’re sticking to comparability, so we’ll limit it.
Set up git
Building an R Shiny app means writing scripts with code and saving these in text files. This works well with git version control. It is easy to compare different versions when the differences are in text. The files are mostly R code files with file extension ‘.R’ like ‘app.R’ which is a special filename recognised by R Shiny as an app file. Whilst one can use just a text editor and R command line, it’s easier to use the popular RStudio IDE (integrated development environment).
RStudio is an end to end solution for R - letting you connect to any data connection, import and export data, create data science work flows, develop web sites and web apps (such as with R Shiny), HTML presentations, write books and scientific articles and publish them to the web or for printing - I used the open-source version.
Layout the file system
In my experience, those new to R Shiny tend to have one or two large files with all the code. More mature coders spread it out into logically named files each with a small amount of code. This modular approach makes it easier to manage the code base, add features and have multiple team members working on the same project at once.
Choose R packages
Shiny is itself an R package - packages extend R’s functionality and are easy to install (a single command install.packages). The community supporting R Shiny is large and increasing every year. Anyone can develop an R package. Quality control for R packages includes standard formats for the files structure, help system, documentation and publishing on CRAN (the standard source for R packages) and most importantly, as R packages are open source anyone can find bugs in the code itself and report them directly to the developer of the package - this means a lot of eyes can look over the code.
Here I used the following packages (in bold) for different components of the dashboard:
web app layout and reactivity
images and titles
I wanted a specific look for the KPI, so I copied the code from an existing function and modified it - this is possible because the packages are open source.
Import and process data with reactives
As with displaying and interacting with tables, there are also multiple packages for importing data. RStudio even has a menu making it easier to import data as it will automatically generate code for you. In building a web app with R Shiny, I like to import data into a reactive. A reactive stores data in the memory of the computer in a way such that when the user makes a change, the reactive responds to the change. In this particular app, I set up a reactive to import the data and another reactive to filter the data when the user moves the sliders. This approach means that the data doesn’t have to be re-imported every time the user makes a change - improving app performance. With more complicated apps, there can be a long trail of reactives, each one responding to something different. This design means that calculations can be minimised to just those necessary - maximising performance. This level of control is critical with larger and more complicated applications.
We already discussed that breaking code up into small files has many benefits. R Shiny supports modular development with the ability to manage module name spaces. This means you can name code objects (such as reactives storing data, or UI components such as buttons) the same name in different contexts instead of always coming up with a unique name for each new object. Coders constantly have to come up with names so this is a life saver as the app grows in size and complexity. This app is small, so the benefit of modular development is not significant - however it’s a good habit to have so I used it anyway - and in a work environment, you never know that the small and simple app might tomorrow be required to add more and more functionality.
Luckily the folks at RStudio make it very easy to deploy an app for free on the internet for anyone to visit and use. Just set up an account with shinyapps.io and publish with a button click in RStudio. There is a limit of 25 hours per month and your app can be hosted in perpetuity.
The code is available here.