% filter(`Donation statistics (UOM)`=='Average annual donations') %>% filter(!GEO=="Canada") %>% filter(!Education == "All education levels") %>% rename(Province = GEO) data$Province[data$Province == "Newfoundland and Labrador"] " />

Dashboards in R with Shiny Dashboard

[This article was first published on R on The Data Sandbox, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In a previous post, I explore the Flex dashboard library for the creation of a clean and interactive dashboard. That post can be found here. Unknown to me at the time, but I sort of skipped over the more natural progression of creating a dashboard with R Shiny. This is my attempt to recreate that dashboard and compare the ease of creation and functionality of the Shinydashboard library.

Application Structure

My shiny dashboard will at heart be a shiny application. I will use the single App file structure rather than the separate UI/Server file structure. The R Studio site has a very good outline on the basic structure for a Shiny Dashboard.

MY application will then be broken down into the following structure:

  • Data Collection
  • Plot Creation
  • UI
  • Server

Before starting to write the Shiny App, the raw data needs to be cleaned and setup to increase application performance.

Data Preparation

The data is the same from the previous dashboard, which was the level of donations for Canadians to charities. The raw data can be found here.

The following code is used to clean up the raw data and is not included in the Shiny App. This code is just used to create an RDS file (compressed data file) that the Shiny App will more easily load.

library(tidyverse)
# Download the data and unzip
download.file("https://www150.statcan.gc.ca/n1/tbl/csv/45100007-eng.zip", "donordata.zip")
unzip("donordata.zip")
# Read the data into R
data = read_csv("45100007.csv")
# Clean up the data
data = data %>%
filter(`Donation statistics (UOM)`=='Average annual donations') %>%
filter(!GEO=="Canada") %>%
filter(!Education == "All education levels") %>% rename(Province = GEO)
data$Province[data$Province == "Newfoundland and Labrador"] <- "Newfoundland"
data$Province[data$Province == "Prince Edward Island"] <- "P.E.I."
# Saved the clean data as an RDS file
saveRDS(data, 'data.rds')

Data Collection

With the data preparation completed, we can now start with writing the application. Shiny Application can be broken down to two parts, reactive and nonreactive. It is important to keep our calculations in the nonreactive part if their values do not change because it will be very demanding on system resources otherwise.

# Loading the libraries
library(shiny)
library(shinydashboard)
library(plotly)
library(tidyverse)
library(leaflet)
library(rgdal)
library(gt)
# Data is loaded into the shiny app from the previously generated RDS file
data = readRDS('data.rds')
# Summary data is created from the loaded data and saved as data2
data2 <- data %>%
group_by(Province) %>%
summarise(Donations = sum(VALUE))
# For regional information for mapping, the rgdal library is used.
library(rgdal)
# The following code will download a regional outlines for maps if the file doesn't exist on the system
if (!file.exists("./src/ref/ne_50m_admin_1_states_provinces_lakes/ne_50m_admin_1_states_provinces_lakes.dbf")){
download.file(file.path('https://www.naturalearthdata.com/http/',
'www.naturalearthdata.com/download/50m/cultural',
'ne_50m_admin_1_states_provinces_lakes.zip'),
f <- tempfile())
unzip(f, exdir = "./src/ref/ne_50m_admin_1_states_provinces_lakes")
rm(f)
}
# The regional data is then loaded into R and some data is edited to make it more inline with the regional data
region <- readOGR("./src/ref/ne_50m_admin_1_states_provinces_lakes", 'ne_50m_admin_1_states_provinces_lakes', encoding='UTF-8', verbose = FALSE
)
data2$Province <- c("Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland and Labrador", "Nova Scotia", "Ontario", "Prince Edward Island", "Québec", "Saskatchewan")

Plot Creation

Just as the data was collected in the nonreactive section, so should the plot creations. This doesn’t mean that the plots won’t be interactive, just that their designs will remain static.

bar_plot <- data %>%
group_by(Province) %>%
summarise(Donations = sum(VALUE)) %>%
ggplot(aes(x = Province, y = Donations, fill = Province)) +
geom_bar(stat = "identity", show.legend = FALSE) +
theme(axis.text.x = element_text(angle = 90), legend.position='none')
# This call was added for illustration
bar_plot

edu_plot <- data %>%
group_by(Education) %>%
rename(Donations = VALUE) %>%
ggplot(aes(y= Donations, x = Education, fill = Education)) +
geom_boxplot() +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
# This call was added for illustration
edu_plot

pie_plot <- data %>%
group_by(Province) %>%
summarise(Donations = sum(VALUE)) %>%
ggplot(aes(x = '', y = Donations, fill = Province)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
theme_void()
# This call was added for illustration
pie_plot

map_leaf <- leaflet() %>%
addTiles() %>%
setView(-74.09, 45.7, zoom = 2) %>%
addPolygons(data = subset(region, name %in% data2$Province), color = "#444444", opacity = 1.0, fillOpacity = 0.75,
fillColor = ~colorQuantile("Greens", data2$Donations)(data2$Donations),
weight = 1)
# This call was added for illustration
map_leaf
To leave a comment for the author, please follow the link and comment on their blog: R on The Data Sandbox.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)