I am a Project Leader working in a data science company. Many of our projects are written in R Shiny. I have never used R before and I believe it is important for me to understand at least on a basic level the challenges that can come with it. In this article I will tell you about my experience with learning R and present to you parts of the code I created.
The opportunity to gain some skills in R Shiny came very quickly. I needed to prepare a report for my manager and an interactive dashboard seemed like the best way to present the huge amount of data I had. All of the developers were really busy in the projects and I needed to work on my own.
The only programming language I have ever touched 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 found out 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.
After two days I had a functional app which showed all of the data I wanted to display. It was easy to analyze the data which could be useful for business decisions. I created something that I knew would be used in the future.
The next day I felt like a pro and I wanted to improve my dashboard even more. And then the problems started. I realized that starting programming in shiny is easy but if you want to customize the app, it gets tricky. I was talking with Pedro, one of my colleagues and he offered help. I thought it was a good opportunity to learn from somebody more experienced. The results definitely exceeded my expectations.
To sum up, in my opinion R Shiny is a great way to quickly display data and create interactive dashboards. But when you want to have a custom design and you are not familiar with css and javascript it might be a bit harder. Another big issue is moving the app to production. My app is used by no more than 2 users concurrently, so It works fine. But If I would like it to be available to a few hundred people, it would require some extra steps. So don’t be afraid to create a Proof of Concept in R. Moving it to production is doable. In one of the projects I lead, we created an R Shiny app which is being used by 500 users.
## Code
Let’s say that we have data that represents a set of industries, companies and the frequency of a certain event for each company:
Data:
ui/server
First I learned about 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 build 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. I don’t want to show all of the code at once because it is quite long. I used (…) in the places that will be explained later.
sliders
First I would like to write about the sliders. The data set used in the app was quite big and I wanted to give the user the possibility to filter it out. The first slider filters on a Freq value from the industries_table.
sliderInput(
inputId = "freq",
label = "Frequency grater then",
min = 0,
max = min(10 , max(as.numeric(industries_table["Freq"][, 1]))),
value = 1
)
Another thing was the number of bars on a first bar chart. I 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 the slider which allows the user to select the number of bars shown on a plot. This way the user could see only top n industries from the data set.
The maximum value of the second slider is defined based on a number of rows in the industries_table table, which depends on the first slider input. I needed to update the maximal value for the second slider every time after user changed the value in the first slider. Here I used the 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.