RMarkdown flexdashboard – Test Drive

The other week I started working with flexdashboard to see if I could create some interactive R Shiny Dashboards. If you have worked with RMarkdown and Shiny before then flexdashboard is fun and easy, if not, then some additional explanations and references may be required to ensure you are having as much fun as I…

The other week I started working with flexdashboard to see if I could create some interactive R Shiny Dashboards. If you have worked with RMarkdown and Shiny before then flexdashboard is fun and easy, if not, then some additional explanations and references may be required to ensure you are having as much fun as I am: (http://rmarkdown.rstudio.com/, flexdashboard,  http://shiny.rstudio.com/ ). 

My ‘Proof of Concept’ was creating a Provider Cost Dashboard, total time to build the database, mock data creator, and dashboard was about 2-3 hours. The purpose of this dashboard is to identify high cost medical providers based on the selected specialty. By running the dashboard in Shiny I can make it interactive; when a provider is selected on the map, the dashboard information is updated immediately. To make this example available to the masses I can not use real data even though this POC was designed to work with actual insurance data. The locations are actually gas stations that are obtained using the Google Places API from R, then the amounts were populated using a simple algorithm.

 

Provider Cost flexdashboard
Provider Cost flexdashboard

I deployed my Shiny flexdashboard example to Digital Ocean  – if there is interest in seeing it live I can insert the link.

Reusability, performance and operating at the correct data grain are all important when building any analytical solution. My source systems are, transactional DBs, warehouse DBs, and definition files that for some reason have not found their way into an enterprise database. It is not viable to construct an online solutions (really any dependable solution) directly from these sources. First I stage the data I require in an analytical form at the correct grain. I have been using Mongodb as the backbone of my Analytical Layer – Implementing an Analytical Layer. Even though this example is shown using mock data I have the ability to populate my Analytical Layer with actual data using an ETL process written in R. Nothing would have to change beyond the loading of the data.

Provider Cost flexdashboard Analytical Layer
Provider Cost Analytical Layer

Let’s look at some code

First things first, install Flexdashboard:
‘devtools::install_github(‘rstudio/flexdashboard’)’

 

My example is using ggplot2, Leaflet (http://leafletjs.com/, https://rstudio.github.io/leaflet/ ) and rmongodb to access the Analytical Layer.

 

As this is an interactive dashboard to run in Shiny, I must add runtime: shiny.

---
title: "Provider Cost"
output: 
  flexdashboard::flex_dashboard:
runtime: shiny
---

 

I include a setup chunk to bring in all required libraries and for function declarations.  For this project the setup has a function to populate the drop down and the Shiny reactive functions for the map, indicators and base charts.

 

```{r setup, include=FALSE}
library(flexdashboard) # devtools::install_github('rstudio/flexdashboard')
library(shiny)
library(data.table)
library(rmongodb)
library(leaflet)
library(rgeos)
library(mapproj)
library(maptools)
library(ggplot2)
library(plyr)
library(ggplot2)
library(scales)


…………… (more) ……………..

 

Sidebar

The ‘sidebar’ is where the input controls are placed. Sliders, dropdowns, etc. If this flexdashboard Rmarkdown document was not using Shiny then this column would be unnecessary.

I use the selectInput function to display the drop down control. The ‘getSpecialty’ function is used to return a distinct provider specialty list from MongoDB, then “General Practice” is set to my selected value. I set selectize to false to avoid the selectize javascript, this was causing some unexpected results when accessing the dashboard from a tablet. This was the quickest solution to my problem; however, additional research is warranted.

 

Column {.sidebar}
-----------------------------------------------------------------------
### Select Specialty
```{r}
inputPanel(
 selectInput('specialty', label = "Select Specialty", choices=getSpecialty(),
 selected = "General Practice", selectize=FALSE)
)
```

 

Rendering the Leaflet

The following code generates the leaflet map and adds green, blue and red markers to indicate the providers for the selected specialty. Green=1 SD below the mean, Blue within 1 SD of the mean, Red =1 SD above the mean.

Clicking a marker will display a popup and fire the ‘click’ event.This event then updates the provider cost trend indicator and comparison chart.

 

### Providers for Selected Specialty
```{r}
output$map <- renderLeaflet({    

 #call reactive function mapData() to get providers with lat, long, 
 and marker indicator

 mpData <- mapData()

 #Set HTML code to diplay when a marker is clicked

 providerPopup <- paste0("<strong>Provider Name: </strong>",
    mpData$ProviderName, "<br/><strong>Total Billed: </strong>",
        dollar(mpData$billed) )

    
 #create map, pass in mapData mpData, add Markers with popup

 map <- leaflet(data=mpData) %>% addProviderTiles("Stamen.TonerLite",
 options = providerTileOptions(noWrap = TRUE)) %>%
 addMarkers(icon = ~costIcons[cost], layerId = ~ProviderID, popup = providerPopup)

 return(map)
})


click_provider <- eventReactive(input$map_marker_click, {

  event <- input$map_marker_click
  #event$id = ProviderID
  return(event$id)
})

click_data <- reactive({

 #Filter the map data by the selected Provider
 return(mpData[ProviderID == click_provider()])

}) 

leafletOutput('map') 

```

 

I am very excited about the possibilities with Shiny, RMarkdown, and Flexdashboard. Being able to create sophisticated interactive self-service deliverable that leverage R… whoa.. we will be able to quickly put information and analytics into the hands of decision makers – something not done well with static reports.  

This was a very simple example of Shiny and Flexdashboard. I just started working with it and hope to add some more in-depth entries if there is an interest.

Please contact me if you have any questions or if you need more details,

Jonathan

5 responses to “RMarkdown flexdashboard – Test Drive”

  1. Hi Jonathan
    I am new to using flexdashboard. Do you know of a simple example with the data and code that I could run to see the basics and then be able to develop it further.

    1. Hi Angela,
      Are you looking for an example using flexdashboard with Shiny or just flexdashboard? The latter is less code as there is no need for Shiny reactive functions.

      1. I think to start with without Shiny would be best thank you

      2. I don’t know of any basic examples so I put one together this morning (hope it helps): https://github.com/Jscholtes128/EconomicIndicators_Flexdashboard . This is just the code, I’ll get an accompanying blog post up later.

  2. Hi Jonathan, thank you for the tutorial. Can you explain why Shiny is needed? Can the dashboard be deployed as is using just flexdashboard or does Shiny add more functionality? I’m not clear on that.

Leave a Reply

Discover more from Stochastic Coder

Subscribe now to keep reading and get access to the full archive.

Continue reading