Notebooks are a fun and easy way to get started with data exploration, analysis, and even initial model building, despite some criticism around it. Imagine you have analyzed an interesting dataset and you wish to share the results with your audience.
Now, you could either share the notebook through GitHub/Nbviewer or export as HTML and host it online. You could also create a PDF report, Powerpoint, Word document or you could share a dashboard 😎
I hear you. Wouldn’t it be time-consuming to design and host dashboards? What if there were tools available that allowed you to leverage most of your code from the notebook?
In this series, I want to take you through the journey of creating dashboards using open source tools. This post will cover building dashboards using Flexdashboard in R.
Both, Data gathering and Exploratory Data Analysis, are huge topics that are covered in great detail under TDS. This post focuses solely on publishing your results in the form of a dashboard. I use the Kaggle glassdoor reviews dataset of over 67k employee reviews from Amazon, Apple, Facebook, Google, Microsoft, and Netflix. The exploratory data analysis was done in R markdown notebook that is available here.
Let’s build some dashboards.
We start by creating a new markdown document in R Studio and selecting “Flex Dashboard” available under “From Template”. If you don’t see Flex Dashboard in the template then you will need to install it using the following and try again.
install.packages("flexdashboard")
This is how the default markdown file look.
The components in the dashboard can be laid out either in a column or row orientation. When using column layout, as in the example above, the components are added in the single column. To add a new component, just start with ### component title and write your R code inside ```{r}….```.
To add your component to the new column, add the dashed line ( — — — — ) under the column or row header, depending on the layout you are using. You can use vertical_layout:fill when you want the components to fill all available browser space or use vertical_layout:scroll if you wish the components to use their natural height. The component width can be controlled using data-width setting.
Next, I will walk you through the steps to create some of the components from Employee Review dashboard shown above.
We load the libraries required to build the charts and the employee review data downloaded from Kaggle.
Our dashboard has multiple pages (Overall, Amazon, Apple, Facebook, Google, Microsoft, Netflix, and Common). The pages can be created using ===== under the page title. We also define the layout for each page using data-orientation=rows. For column layout, you can use data-orientation=columns.
Overall {data-orientation=rows}
=====================================
We create a row to display overall ratings of each company using Gauges. Flexdashboard supports a number of components listed here.
# This is how you create a gauge for a value between the range 0.0
# and 5.0. You can define the color range in gaugeSectors
rating <- 3.0
gauge(rating, min = 0, max = 5, symbol = '%', gaugeSectors(
success = c(3.8, 5.0), warning = c(3.0, 3.8), danger = c(0.0, 3.0)
))
The above code will create a single gauge. In our case, we need to do it for each company, so I have converted it into a function that takes the company name as an input parameter and returns a gauge.
Next, we use leaflet to plot the reviewer location. The dataset from Kaggle only includes the location name of reviewers , but we need latitude and longitude to generate the location map. I used the geocoding API from Google to download the coordinates for all locations that had more than 25 reviews. The only reason I did that was to avoid stay under the free quota but now checking back I think I could have included every location from the dataset.
Similarly, we can keep adding the components depending on how we wish to lay them out. The next plot shows the heatmap of ratings on different topics across all companies.
We can add a new page (e.g Google) using the following text and continue adding charts.
Google {data-orientation=rows}
=====================================
The boxes shown in the first row are called ValueBox, another in-built component, and we can create one using
### Work/Life Balance
valueBox(4)
And since we are going to use it for different kind of ratings for each company, I have turned the code into a function to display the value along with color coding i.e show background as warning if the value is less than the benchmark else show the value in primary color.
And thats it. We can continue adding new pages and components or plots.
Hopefully, this post has managed to show how simple it is to create dashboards using flexdashboard in R. The flexdashboard documentation is an excellent resource to guide you through different layouts and components available. The dashboard demo is available online and code is available on my GitHub repo.