Shiny App for Hurricane Matthew Simulation

Shiny is the amazing web application framework for R. Today I want to illustrate an example about how it works and how to change the framework theme, a new shiny option incorporated recently.

First, we load all libraries we are going to use. In this case we will plot a map with hurricane Matthew evolution, so we will need rvest to obtain data through web scraping, and ggplot2 and ggmap to plot the background map.


Next step is to download data. We have to make some changes preventing errors with punctuation, setting a correct factor order and reading adequately dates from dataframe table.

# Download Matthew Hurricane Data
matthew <- read_html("")
nodes <- html_nodes(matthew,"#stormList")
table <- html_table(nodes)[[1]]

# Avoid punctuation and white spaces
names(table) <- gsub("([^[:alnum:]])", "", names(table))

# Change factor order
table$StormType <- factor(table$StormType)
HurricaneOrder <- order(as.numeric(gsub("([^[:digit:]])", "", levels(table$StormType))),decreasing = T)
table$StormType <- factor(table$StormType, levels(table$StormType)[HurricaneOrder])

# Avoid NA(s) in some locales, so set the C locale
Sys.setlocale("LC_TIME", "C")
table$Date <- as.Date(table$Date, "%b/%d/%Y")

Then, a shiny app is composed by a user interface script and a server script. First one controls the layout and appearance of your app, and second one contains the instructions that your computer needs to build your app. Here we build the app in the same script, with a theme selector, a slider input to choose a date and a panel with the ggmap plot.

# ShinyApp
ui = fluidPage(

titlePanel("Hurricane Matthew "),

sliderInput("date1", "Date:",
value=as.Date("2016-09-28"), animate = T)
server = function(input, output) {

dataOutput <- reactive({
table$type <- ifelse(table$Date==input$date1, 0.7, 0); table

FloridaMap <- qmap("florida", zoom=4)

output$mapPlot <- renderPlot({

p <- FloridaMap +
geom_point(aes(x=Lon, y=Lat, size=Windmph, colour=StormType, alpha=type),
data=dataOutput()) +
labs(title="Hurricane Matthew ", x="Longitude", y="Latitude", size="Wind Speed") +
scale_colour_brewer(palette = "Spectral")
}, height=600)


You can see more shiny app examples here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s