Created
July 31, 2016 21:10
-
-
Save erzk/bb3d6318255e85b81f3d35935120dceb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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