10/23/2017

Introduction

The goal of the project is to let users explore interactively the Air Quality dataset for 2017 provided by EPA (source data here).

I've set up a Shiny application (direct link here) with three important components:

  • Dynamic loading of variables in the drop-down menu
  • Readout of selected data points on the associated scatterplot
  • Application of Leaflet to display geographical locations of air quality sensors

Why Shiny instead of code run locally?

We can download and process the air quality dataset, like below.

library(dplyr); library(lubridate)
download.file("https://aqs.epa.gov/aqsweb/airdata/annual_conc_by_monitor_2017.zip",
              destfile = "annual_conc_by_monitor_2017.zip")
unzip("annual_conc_by_monitor_2017.zip")
anc_sub <- tbl_df(read.csv("annual_conc_by_monitor_2017.csv")) %>%
  filter(Parameter.Name %in% c("PM2.5 - Local Conditions", "Ozone", "Sulfur dioxide")) %>%
  mutate(X1st.Max.DateTime = ymd_hm(X1st.Max.DateTime))

And then apply calculations, like linear model, for a given subset.

my_model <- with(anc_sub, lm(X1st.Max.Value ~ X1st.Max.DateTime))
summary(my_model)$coefficients
##                        Estimate   Std. Error   t value     Pr(>|t|)
## (Intercept)        4.412931e+03 6.801735e+02  6.487949 9.202738e-11
## X1st.Max.DateTime -2.952874e-06 4.573397e-07 -6.456630 1.131112e-10

This is resource-consuming (run on our side) and code-heavy.

Benefits of Shiny

The user can easily interact with data, calculations are run on the server side.

Example: high sulfur dioxide values have been recorded near a hawaiian volcano.

Screenshot - example

Highlights

  • renderUI() is a very useful function for generating input structures dynamically.
# ui.R
selectInput(inputId = "sensor_type", 
            label = "A. Choose a sensor type:",
            choices = c("PM2.5 - Local Conditions", "Ozone", "Sulfur dioxide")),
uiOutput("choose_measure")
#server.R
output$choose_measure <- renderUI({
  range <- anc_sub %>% filter(Parameter.Name == input$sensor_type) %>% pull(Metric.Used)
  selectInput("selected_measure", "Choose a measure:", unique(range))
})
  • Leaflet is available for Shiny and well integrated.
  • Developing the client-server logic and code was really fun and productive.
  • see my github for the code of the Shiny app, and of this presentation, including the CSS stylesheet.