# Load necessary libraries
library(httr)
library(jsonlite)
library(knitr)
The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs You’ll need to start by signing up for an API key.
api_key <- "7906c20e148144d691200e10b722a0fb"
Your task is to choose one of the New York Times APIs
url <- "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json"
url <- paste0(url, '?api-key=', api_key)
Construct an interface in R to read in the JSON data
json_data <- fromJSON(url)
Transform it to an R dataframe
bestseller_book <- json_data$results
colnames(bestseller_book)
## [1] "title" "description" "contributor"
## [4] "author" "contributor_note" "price"
## [7] "age_group" "publisher" "isbns"
## [10] "ranks_history" "reviews"
title_author <- subset(bestseller_book[1:10, c('title', 'author')])
kable(title_author)
| title | author |
|---|---|
| “I GIVE YOU MY BODY …” | Diana Gabaldon |
| “MOST BLESSED OF THE PATRIARCHS” | Annette Gordon-Reed and Peter S Onuf |
| #ASKGARYVEE | Gary Vaynerchuk |
| #GIRLBOSS | Sophia Amoruso |
| #NEVERAGAIN | David Hogg and Lauren Hogg |
| $100 STARTUP | Chris Guillebeau |
| $20 PER GALLON | Christopher Steiner |
| ’57, Chicago | Steve Monroe |
| ‘ROCK OF AGES:’‘ROLLING STONE’‘HISTORY OF ROCK AND ROLL’ | GEOFFREY STOKES, KEN TUCKER’ ’ED WARD |
| ‘THE HIGH ROAD TO CHINA: GEORGE BOGLE, THE PANCHEN LAMA AND THE FIRST BRITISH EXPEDITION TO TIBET’ | KATE TELTSCHER |