Getting Data into R Part 3 - Web APIs

Feb 25, 2017 · 368 words · 2 minutes read APIsRdata processing

Application programming interfaces (APIs) are a general term to describe methods for communicating between different software components, such as facilitating the user of a low-level library by a higher-level language. Web APIs provide methods for communicating with a web service by making requests through designated endpoints. Since web applications already make use of APIs, they’re a great choice for getting data for analysis or posting a result.


httr

httr provides methods for making requests to a web API. The main functions, which correspond to the REST verbs, are GET, POST, PUT, and DELETE.

Suppose we want to find the lat/lon coordinates for an address. Google provides a geocoding API for their maps service, which we can use. Note you’ll need an API key to follow along. You can get one here and stick it in your .bash_profile as an environment variable.

From the Google Maps Geocoding API documentation, we can see that our request should include either a string with the address we want to geocode or the components of the address (e.g. number, street name, city, state, zipcode). I’m just going to stick with the address as a string. The request also needs the API key to authenticate.

We can write a function that takes an address string and passes it to the Google Maps Geocoding API, parse the output, and return a list with the lat/lon coordinates.

library(httr)
library(magrittr)

geocode <- function(address){
    query <- list(address = address,
                  key = Sys.getenv("GOOGLE_MAPS_KEY"))
    res <- GET(url = "https://maps.googleapis.com/maps/api/geocode/json",
               query = query)
    stop_for_status(res)
    content(res) %>%
      extract2("results") %>%
      extract2(1) %>%
      extract2("geometry") %>%
      extract2("location")
}

geocode(address = "333 W 7TH ST., ROYAL OAK, MI 48067")
# $lat
# [1] 42.48433
#
# $lng
# [1] -83.14735

Now we have the lat/lon coordinates for our address provided by a web service.


Web APIs are another great way to get data into R, especially when there is an existing service you can use. The same web service can be used across platforms. That means your analysis code can use the same API, along with it’s data, as an Android, iOS, or web app. This is especially useful when you have a calculation or process that can be done server side (instead of multiple implementations).