R Shiny is a great way to quickly display data and create interactive dashboards, and it is the backbone of many Appsilon Data Science’s projects. As a Project Leader I needed to understand the challenges that come with working in this environment, even though I personally have never used R before. In this article I will discuss my experience with learning R and show you how to build a Shiny dashboard in just two days without any prior knowledge.
The only programming language I have ever got any hands-on experience with is Python and I would say that I know it on a “google it and try it” level. I thought learning R would be quite a challenge. Fortunately I discovered that there are many examples of R Shiny code out there which I could use, so I started immediately. I was really surprised how easy it is to start writing code in R.
The opportunity to apply this knowledge and develop my skills in R Shiny presented itself quickly. I needed to prepare a report for my manager and an interactive dashboard seemed like the best way to present the large amount of data I was working on.
Within just two days I had a functional app which showed all of the data I wanted to display. The dashboard made it easy to analyze the data in a way that would be useful for business decisions. I created something that I knew would be used in the future.
On the second day, I already felt more confident and I wanted to develop my dashboard further. And then the problems started. I realized that starting programming in Shiny is easy but it gets tricky if you want to customize the app. I thought it was a good opportunity to learn from somebody more experienced and got the help from one of our developers. The results definitely exceeded my expectations.
Ultimately, R Shiny is an excellent tool for quickly creating visually appealing and useful dashboards and is relatively easy to learn. However, there are two major challenges to deploying more advanced solutions.
I still would encourage developers to create Proof of Concept solutions in R – moving them to production certainly is possible. In one of the projects I lead, we created an R Shiny app which is being used by 500 users. Productionisation is a challenging exercise indeed, and it is one of the specialties of Appsilon Data Science. If you want to discuss building enterprise decision support dashboards, feel free to reach out.
The slide below provides a hint at what supports a solid production-ready Shiny application. Read on for the details of my experience with code samples which should help you build your first dashboard.
Let’s say that we have data that represents a set of industries, companies and the frequency of a certain event for each company:
To get started I looked into the structure of an R Shiny app. I found out that the main file is divided into two parts, ui and server. The ui is built from sidebarPanel and mainPanel.
The sidebarPanel contains input controls which can be passed to the mainPanel. The mainPanel contains items defined on a server side which we would like to display in the app.
As you can see below, my app contains four inputs, two plots, one table and a text field. For the sake of clarity I am not showing the entire code at once. I used (…) for the lines of code that will be explained later.
The data set used in the app was quite big and I wanted to give the user the possibility to filter it out using sliders. The first slider allows to define a filter based on the values from the Freq column in the industries_table.
inputId = "freq",
label = "Frequency grater then",
min = 0,
max = min(10 , max(as.numeric(industries_table["Freq"][, 1]))),
value = 1
I then realized that the user might want to focus only on the industries which are the most common in the data set. That is why I created a slider which allows the user to select the number of bars shown on the first bar chart. This way the user could see only top n industries from the data set on the plot.
The maximum value of the second slider is based on the number of rows in the industries_table table, which depends on the first slider input. I needed to update the maximum value for the second slider every time after the user changes the value in the first slider.
Here I used a reactive expression called observeEvent which allowed me to monitor the changes and update the slider input if necessary. The grayed out part is responsible for preparing the data and will be explained later. The code below tells us that we will monitor the freq slider and change the maximum and default value of no_of_industries slider.