Introduction

This code looks into ways to make it simple to create interactive dashboards for R by combining R Markdown and flexdashboard


Content Overview

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, -


Why You Should Care

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


Learning Objectives

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 Chunk

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.


Further Exposition

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.


Basic Example

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.

Load Dataset

data <- read.csv("~/Desktop/ChurnHVC.csv")

Assign Colors

mycolors <- c("blue", "#FFC125", "darkgreen", "darkorange")

Interactive Data Visualization

Row

High Value Customer Analysis

valueBox(paste("Churned"),   # Create Tab 1
         color = "danger")

Churned

High Value Customer Count

valueBox(length(data$Customer.ID),  # Create Tab 2
         icon = "fa-user")

578

Average Revenue from HVCs

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")))

Los Angeles

valueBox(sum(data$City == "Los Angeles"),     # Create Tab 4
         icon = 'fa-user')

24

San Diego

valueBox(sum(data$City == "San Diego"),       # Create Tab 5
         icon = 'fa-user')

15

San Francisco

valueBox(sum(data$City == "San Francisco"),      # Create Tab 6
         icon = 'fa-user')

10

Sacramento

valueBox(sum(data$City == "Sacramento"),        # Create Tab 7
         icon = 'fa-user')

8

San Jose

valueBox(sum(data$City == "San Jose"),        # Create Tab 8
         icon = 'fa-user')

7


Advanced Examples

More specifically, this can be used to create basic charts and more advanced charts

Row

HVCs by City

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'))
p1

Customer status by Cities

p2 <- 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))
p2

Total Revenue by City

p3 <- 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"))
p3

Row

Scatter Plot of Month Vs Mileage

p4 <- 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"))
p4


We can create other tabs to help enhance the visualization

Data Table

datatable(data,
          caption = "High Value Customers",
          rownames = T,
          filter = "top",
          options = list(pageLength = 25))

Summary

Column

Most Revenue from customer

valueBox(max(data$Total.Revenue),
         icon = "fa-user" )

11979.34

Average Revenue

valueBox(round(mean(data$Total.Revenue),
               digits = 2),
         icon = "fa-area-chart")

9239

Average Monthly Charge

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:

Outcome

When the codes anove are ran in r markdown above, the result will be an interactive dashboard like the below; Dashboard

This dashboard aids in decision-making, improves user empowerment, and makes it easier to spot trends quickly.

Further Resources

Learn more about [R markdown, flexdashboard, maven churn dataset] with the following:




Works Cited

This code through references and cites the following sources: