This code looks into ways to make it simple to create interactive dashboards for R by combining R Markdown and flexdashboard
Dashboard is the combination of data and visualizations which helps in decision making. In this tutorial, we will generate a dashboard using R Markdown. Our focus will be on the following, -
The numerous benefit of R markdown makes it very important to learn. Some of the benefits inlude; -Readable by humans -Syntax is simple. -Simple to Change -Export formats that are adaptable -Simple to distribute
You will be able to do the following at the end of this activity:
-List the advantages of creating reports with R Markdown. -Describe how R Markdown is useful in Open Science approaches. -Describe how R Markdown can help your research.
Code chunks are formed by three back-ticks and curly brackets containing a lowercase “r.” Three back-ticks close the chunk. You can insert a new chunk by typing it, using the keyboard shortcut “Ctrl + Alt + I (or Cmd + Shift + r in Mac), or by clicking the green ‘insert a new code chunk’ icon at the top of your script editor. An R Markdown script can have multiple code”chunks” - these are sections of the script where you can write multiple-line R code and they function similarly to mini R scripts.
You can write narrative text outside of a R code “chunk.” You can italicize text by surrounding it with one asterisk (*), or bold text by surrounding it with two asterisks (**), as described on the Reports with R Markdown page. Remember that bullets and numbering schemes are affected by newlines, indentation, and the use of two spaces at the end of a line.
A basic example shows how to create a dashboard for a fictitious telecom company, with the aim of analyzing its churning activities. Firstly, create a new R markdown and follow the steps below. For better understanding, view the .Rmd file attached in the Link section.
data <- read.csv("~/Desktop/ChurnHVC.csv")mycolors <- c("blue", "#FFC125", "darkgreen", "darkorange")valueBox(paste("Churned"), # Create Tab 1
color = "danger")Churned
valueBox(length(data$Customer.ID), # Create Tab 2
icon = "fa-user")578
gauge(round(mean(data$Total.Revenue), # Create Tab 3
digits = 6),
min = 7999,
max = 12000,
gaugeSectors(success = c(10000, 12000),
warning = c(8000, 9999),
danger = c(0, 7999),
colors = c("green", "yellow", "red")))valueBox(sum(data$City == "Los Angeles"), # Create Tab 4
icon = 'fa-user')24
valueBox(sum(data$City == "San Diego"), # Create Tab 5
icon = 'fa-user')15
valueBox(sum(data$City == "San Francisco"), # Create Tab 6
icon = 'fa-user')10
valueBox(sum(data$City == "Sacramento"), # Create Tab 7
icon = 'fa-user')8
valueBox(sum(data$City == "San Jose"), # Create Tab 8
icon = 'fa-user')7
More specifically, this can be used to create basic charts and more advanced charts
p1 <- data %>% # chart 1
group_by(City) %>%
summarise(Customer.ID = n()) %>%
plot_ly(x = ~City,
y = ~Customer.ID,
color = "blue",
type = 'bar') %>%
layout(xaxis = list(title = "HVCs by City"),
yaxis = list(title = 'Customer.ID'))
p1p2 <- data %>% # chart 2
group_by(Customer.Status) %>%
summarise(City = n()) %>%
filter(City>5) %>%
plot_ly(labels = ~Customer.Status,
values = ~City,
marker = list(colors = mycolors)) %>%
add_pie(hole = 0.1) %>%
layout(xaxis = list(zeroline = F,
showline = F,
showticklabels = F,
showgrid = F),
yaxis = list(zeroline = F,
showline = F,
showticklabels=F,
showgrid=F))
p2p3 <- plot_ly(data, # chart 3
x = ~City,
y = ~Total.Revenue,
text = paste("City:", data$City,
"Total.Revenue:",
data$Total.Revenue),
type = "bar") %>%
layout(xaxis = list(title="City"),
yaxis = list(title = "Total.Revenue"))
p3p4 <- plot_ly(data, x=~Total.Revenue) %>% # chart 4
add_markers(y = ~Total.Charges,
text = ~paste("Total.Charges: ", Total.Charges),
showlegend = F) %>%
add_lines(y = ~fitted(loess(Total.Charges ~ Total.Revenue)),
name = "Loess Smoother",
color = I("#FFC125"),
showlegend = T,
line = list(width=5)) %>%
layout(xaxis = list(title = "Total.Revenue"),
yaxis = list(title = "Total.Charges"))
p4We can create other tabs to help enhance the visualization
datatable(data,
caption = "High Value Customers",
rownames = T,
filter = "top",
options = list(pageLength = 25))valueBox(max(data$Total.Revenue),
icon = "fa-user" )11979.34
valueBox(round(mean(data$Total.Revenue),
digits = 2),
icon = "fa-area-chart")9239
valueBox(round(mean(data$Monthly.Charge), digits = 2),
icon = "fa-area-chart")101.29
Most notably, we can enable shiny in a flexdashboard by adding the YAML parameter runtime: shiny at the same indentation level as output:, as below:
When the codes anove are ran in r markdown above, the result will be
an interactive dashboard like the below;
This dashboard aids in decision-making, improves user empowerment, and makes it easier to spot trends quickly.
Learn more about [R markdown, flexdashboard, maven churn dataset] with the following:
Resource I- R markdown
Resource II- Flexdashboard
Resource III-MavenChurnDataset
This code through references and cites the following sources:
Dr. Bharatendra Rai (2018). R markdownfile
Dr. Bharatendra Rai (2018). Youtube