Skip to content

Instantly share code, notes, and snippets.

@agoldst
Created May 19, 2014
Embed
What would you like to do?
Query the Nobelprize.org API to create a CSV table of all the literature laureates.
library("httr")
r_lits <- GET("http://api.nobelprize.org/v1/prize.json",query=list(category="literature"))
laureates <- content(r_lits,"parsed")$prizes # JSON
ids <- sapply(laureates,function (psn) {
psn$laureates[[1]]$id
})
lines <- character(length(ids) + 1)
names(lines) <- c("header",ids)
lines[1] <- ""
for (i in seq_along(ids)) {
message("Getting id ",ids[i])
r <- GET("http://api.nobelprize.org/v1/laureate.csv",query=list(id=ids[i]))
lau <- strsplit(content(r,encoding="utf-8"),"\n")[[1]]
if (lines[1] == "") {
lines[1] <- lau[1]
}
if (lines[1] != lau[1]) {
warning("Deviant header for id ",ids[i],": ", lau[1])
}
lines[i + 1] <- lau[2]
Sys.sleep(3)
}
writeLines(lines,"laureates.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment