Please note that this book is a work in progress, the equivalent of a pre-alpha release. All the code should work, because if it didn’t the site could not be built. But there is still a lot of work to do to explain the historical methods under discussion. Feel free to leave feedback as issues on the GitHub repository, or to e-mail me.

Downloading Files with CURL

Accessing APIs

library(httr)

Httpbin

As you are learning to work with APIs, and in particular if you are writing a package or function that wraps an API, you may find the service httpbin to be useful. That service provides many sample endpoints so that you can experiment with APIs. These endpoints will in most cases return the data that you posted to them, so you can see exactly what the service sees, not just what you think it should see.1

For example, using the ip endpoint for httpbin (which you can visit in your browser to see the JSON that is returned), we can write a simple function that discovers your IP address.

what_is_my_ip <- function() {
  require(httr)
  r <- GET("http://httpbin.org/ip")
  warn_for_status(r) # Print a warning if request was unsuccessful
  content(r)$origin
}

what_is_my_ip()
## [1] "72.192.238.10"

JSON

XML

See the section on scraping websites.

APIs of interest to historians

The Digital Public Libary of America (DPLA) has a very useful and well documented API.

Packages that provide API wrappers


  1. I found this service through the vignette for the httr package.