"Airports in the United States"

  • This was originally an r-markdown file, now converted to notebook format, from John Lam's repo here. I've also adapted some of the plotting because javascript+html is quite a bit different in a jupyter notebook, especially with the R kernel.

For packages that will be used here you may have to go to the terminal or R GUI you use and set these appropriately:

  1. setRepositories()
  • chooseCRANmirror()

In [26]:
# add a local lib to the R library path (for permission purposes)
local_lib = '/Users/micheleenharris/lib/R' # this must exist!
.libPaths(c(.libPaths(), local_lib))

# package to grab url data
install.packages('RCurl', repos = "http://cloud.r-project.org", lib = local_lib)

# some interactive plotting packages
install.packages(c("DT", "htmlwidgets", "leaflet"),
                repos = "http://cloud.r-project.org",
                lib = local_lib)


The downloaded source packages are in
	‘/private/var/folders/_f/464k2bb144v0gwg6lys9db780000gn/T/RtmptCBksm/downloaded_packages’
also installing the dependency ‘png’

Warning message:
In install.packages(c("DT", "htmlwidgets", "leaflet"), repos = "http://cloud.r-project.org", : installation of package ‘png’ had non-zero exit statusWarning message:
In install.packages(c("DT", "htmlwidgets", "leaflet"), repos = "http://cloud.r-project.org", : installation of package ‘leaflet’ had non-zero exit status
The downloaded source packages are in
	‘/private/var/folders/_f/464k2bb144v0gwg6lys9db780000gn/T/RtmptCBksm/downloaded_packages’

Let's load a list of airport data from https://github.com/jpatokal/openflights


In [9]:
# TIP:  often you'll want some data externally, but can not upload a file to jupyter system
#   In this case I pull directly from a raw file online (like a repo file here)

library(RCurl)

airports <- getURL("https://raw.githubusercontent.com/jflam/VSDemos/master/RTVSDemos/airports.dat")
airports <- read.csv(text = airports, 
                     header = FALSE)
colnames(airports) <- c("ID", "name", "city", "country", "IATA_FAA", "ICAO", "lat", "lon", 
                        "altitude", "timezone", "DST", "Region")

This data set contains airports from all over the world. Let's get only the airports from the United States.


In [12]:
usa_airports <- subset(airports, country == "United States")

# DT is a really nifty interactive table library with pagination
library(DT)
table <- datatable(usa_airports[,c("name", "city", "country", "IATA_FAA", "lat", "lon", "altitude")])

# Here is the trick to get an interactive graphic to work in notebooks
# 1. load htmlwidgets
# 2. save interactive object to an html file with saveWidget
# 3. load the html back into the notebook and into an iframe element for display with IRdisplay

library(htmlwidgets) # lots of cool widgets associated w/ this (http://www.htmlwidgets.org/)

fname <- "something.html"

# selfcontained = FASLE means: save a file with external resources placed in an adjacent directory
#  do this just in case there are external resources
saveWidget(table, file = fname, selfcontained = F)
IRdisplay::display_html(paste("<iframe src=' ", fname, " ' width = 100% height = 400>"))


Error in library(htmlwidgets): Package ‘htmlwidgets’ version 0.6 cannot be unloaded

Let's generate a static map containing the location of all of the airports in the USA


In [22]:
library(ggmap)
map <- get_map(location = "Seattle, WA", zoom = 3)
ggmap(map) + geom_point(aes(x = lon, y = lat), data = usa_airports, alpha = 0.25)


Error in library(ggmap): there is no package called ‘ggmap’
Error in eval(expr, envir, enclos): could not find function "get_map"
Error in eval(expr, envir, enclos): could not find function "ggmap"

Let's generate a zoomable, pannable map using the leaflet package. Here you can see airports that belong to the USA that aren't geographically within the borders of the United States.


In [23]:
library(leaflet)
m <- leaflet(data = usa_airports) %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addCircles(~lon, ~lat, popup = ~name)

# Trick again - probably should make into a fxn
library(htmlwidgets)
fname = 'tmp.html'
saveWidget(m, file = fname, selfcontained = F)
IRdisplay::display_html(paste("<iframe src=' ", fname, " ' width = 100% height = 400>"))


Error in library(leaflet): there is no package called ‘leaflet’
Error in eval(expr, envir, enclos): could not find function "leaflet"
Error in library(htmlwidgets): Package ‘htmlwidgets’ version 0.6 cannot be unloaded
Error in resolveSizing(x, x$sizingPolicy, standalone = standalone, knitrOptions = knitrOptions): object 'm' not found

In [25]:
library(leaflet)
pal <- colorQuantile("YlOrRd", NULL, n = 8)
m <- leaflet(usa_airports) %>% 
  addTiles() %>%
  addCircleMarkers(color = ~pal(lat))

# Trick again just for good measure
library(htmlwidgets)
fname = 'tmp.html'
saveWidget(m, file = fname, selfcontained = F)
IRdisplay::display_html(paste("<iframe src=' ", fname, " ' width = 100% height = 400>"))


Error in library(leaflet): there is no package called ‘leaflet’
Error in eval(expr, envir, enclos): could not find function "colorQuantile"
Error in eval(expr, envir, enclos): could not find function "leaflet"
Error in library(htmlwidgets): Package ‘htmlwidgets’ version 0.6 cannot be unloaded
Error in resolveSizing(x, x$sizingPolicy, standalone = standalone, knitrOptions = knitrOptions): object 'm' not found

Created by a Microsoft Employee based on work by another Microsoft Employee.

The MIT License (MIT)
Copyright (c) 2016 Micheleen Harris


In [ ]: