Skip to content

Instantly share code, notes, and snippets.

@erzk
Created July 31, 2016 21:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save erzk/bb3d6318255e85b81f3d35935120dceb to your computer and use it in GitHub Desktop.
# load this package to use HTTP verbs
library(httr)
postcode_lookup <- function(postcode) {
r <- GET(paste0("https://api.postcodes.io/postcodes/", postcode))
warn_for_status(r)
content(r)
}
# returns a list
pc_content <- postcode_lookup("EC1Y 8LX")
# transform into a data frame
pc_result <- pc_content[[2]]
take_names <- setdiff(names(pc_result), 'codes')
pc_result[sapply(pc_result, is.null)] = list(NA)
pc_df <- cbind(as.data.frame(pc_result[take_names]),
as.data.frame(pc_result$codes))
# create an interactive map
library(dplyr) # load pipes
library(leaflet) # load maps
leaflet(pc_df) %>%
addTiles() %>%
addCircles(lng = ~longitude, lat = ~latitude, popup = ~postcode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment