Introduction

Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it into an R DataFrame.

Loading Libraries

The following libraries are required for running this code

library(glue)
library(httr)
library(jsonlite)
library(knitr)

NY Times books API

I decided to use the NY Times books API, which gives the user data on book rankings by category or what they call list, for a given week. It’s the data that comprises makes up their Best Seller List.

Construct URL

First we want to construct the URL so we pull the data we want with the parameters we want.

url_string = "https://api.nytimes.com/svc/books/v3/lists/{date}/{list}.json?api-key={api_key}"

The parameters include date, list, and api_key. For the purposes of this assignment I used the last day before the COVID-19 lockdown in New York, the list of books was hardcover-nonfiction and my api-key.

api_key <- params$key
list <- "hardcover-nonfiction"
date <- "2020-03-14"

For the list parameter you could choose from the following options:

x
combined-print-and-e-book-fiction
combined-print-and-e-book-nonfiction
hardcover-fiction
hardcover-nonfiction
trade-fiction-paperback
mass-market-paperback
paperback-nonfiction
e-book-fiction
e-book-nonfiction
hardcover-advice
paperback-advice
advice-how-to-and-miscellaneous
hardcover-graphic-books
paperback-graphic-books
manga
combined-print-fiction
combined-print-nonfiction
chapter-books
childrens-middle-grade
childrens-middle-grade-e-book
x
childrens-middle-grade-hardcover
childrens-middle-grade-paperback
paperback-books
picture-books
series-books
young-adult
young-adult-e-book
young-adult-hardcover
young-adult-paperback
animals
audio-fiction
audio-nonfiction
business-books
celebrities
crime-and-punishment
culture
education
espionage
expeditions-disasters-and-adventures
fashion-manners-and-customs
x
food-and-fitness
games-and-activities
graphic-books-and-manga
hardcover-business-books
health
humor
indigenous-americans
relationships
mass-market-monthly
middle-grade-paperback-monthly
paperback-business-books
family
hardcover-political-books
race-and-civil-rights
religion-spirituality-and-faith
science
sports
travel
young-adult-paperback-monthly

Reading data from API

When we first read the raw.data, we get some information about the link as well as the link results. From the results list we can grab the books data frame.

raw.data <- fromJSON(rawToChar(GET(glue(url_string))$content))
book.data <- raw.data$results$books

Creating an R DataFrame

Finally we can simplify the book data frame that the API gives us to a data frame with the information we want in it.

book.rank.df <-
  book.data[,c("rank",
               "rank_last_week",
               "weeks_on_list",
               "title",
               "author",
               "description",
               "primary_isbn13")]

The final result

Top 5 NYTimes Ranked Hardcover Nonfiction Books for the week of 2020-03-09
rank rank_last_week weeks_on_list title author description primary_isbn13
1 0 1 THE SPLENDID AND THE VILE Erik Larson An examination of the leadership of the prime minister Winston Churchill. 9780385348713
2 1 11 THE MAMBA MENTALITY Kobe Bryant Various skills and techniques used on the court by the late Los Angeles Lakers player. 9780374201234
3 0 1 UNKNOWN VALOR Martha MacCallum The Fox News anchor weaves stories of combat veterans who fought during World War II. 9780062853851
4 0 1 THE HOPE OF GLORY Jon Meacham The Pulitzer Prize-winning biographer delves into the seven last sayings of Jesus as recorded in the Gospels. 9780593236666
5 3 4 OPEN BOOK Jessica Simpson with Kevin Carr O’Leary The singer, actress and fashion designer discloses times of success, trauma and addiction. 9780062899965