Writing

R Shiny App for Merced Weather Data Visualization

· rshiny, ggplot2, dplyr, dataviz

A looong time ago, I wrote a post with Merced weather data visualization inspired by Tufte’s NYC weather analysis and here is the long awaited (is.na = T) follow-up of that post. R code to build the chart is located here and over at the github repo I have created for the shiny app. I wanted to write an interactive application using Shiny, RStudio’s web application framework for R, and the Merced weather visualization seemed like a great candidate. In the chart below you see shiny app that I have written in action – an interactive version of my previous posts visualizations. Users can select a range of years of temperature data to form the background, anywhere between years 2000 and 2017 inclusive.

This app is hosted via my account over at shinyapps.io, free tier (which is also the reason why the app is sometimes slow to load) of which allows 25hours of network time/month. Code and further documentation on how to write your own shiny app are in github repo linked above. I would also recommend taking DataCamp’s excellent course or video and written tutorials provided by shiny team itself.

Shop Talk

A Shiny app requires a minimum of two code modules named ui.R and server.R. I got started writing the app by copying those two modules from lesson 2 of the RStudio Shiny tutorial to my local folder and then making the necessary changes. I also added a sliderInput user-interface to allow the user to select the start_year to bracket data upto and including 2017. By default the weather visualization displays the daily record highs and lows and also the daily average highs and lows in the background. I added two checkboxInputs to allow the user to hide those background displays. The code for the original visualization post uses a file, viz_temps.R, to allow it to work within the Shiny framework. Function viztemps() which is defined in viz_temps.R is invoked from server.R and makes the magic happen.

ui.R

library(shiny)

# Define UI for application
shinyUI(fluidPage(

  # Application title
  titlePanel("Merced,CA 2014 Temperatures"),

  # Show a plot of the generated distribution
  plotOutput("MercedTemps"),

  hr(),

  # Sidebar with a slider input for the number of bins
  fluidRow(
    column(5,
      h4("Data Range (Start Year to 2017)"),
      sliderInput('start_year',
                  'Select StartYear',
                  min = 2000,
                  max = 2017,
                  value = 2008),
      offset=1
    ),
    column(5,
      checkboxInput('hide_hilows', 'Hide background daily record highs and lows bars'),
      checkboxInput('hide_avgs', 'Hide background average daily highs and lows bars'),
      offset=1
    )
  )
))

server.R

library(shiny)

# Preprocessing and summarizing data
library(dplyr)

# Visualization development
library(ggplot2)

# For text graphical objects (to add text annotation)
library(grid)

source("viz_temps.R")

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  output$DublinTemps <- renderPlot({
    viztemps(input$start_year,
             input$hide_hilows,
             input$hide_avgs
    )
  })
})

To get the app over on this post I simply embedded it using HTML iframe tag using following code.

<iframe src="https://rgupta.shinyapps.io/shiny/" style="border: none; width: 100%; height: 700px"></iframe>

And that’s it, with simple modifications to ui.R and server.R provided in tutorials and writing a viztemp function that takes raw data and creates plots using ggplot which are then communicated with ui.R and server.R an interactive version of my previous plot has been created.

Next on the list:

  • An ode to joy-plot - Creating joy plots ( aka ridgeline plot) for weather data. Mainly doing this for the pun in the title.
  • Weather in Cloud - Taking the weather data to AWS and creating a dockerized container. Again, mostly for the title pun.
  • ScraPy vs Octoparse vs BeautifulSoup
  • Am I going to be delayed? : Using ML to determine flight delays.
← All writing