The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis
The 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.
library(knitr)
library(jsonlite)
library(httr)
library(glue)
library(dplyr)
library(shiny)
Construct an interface in R to read in the JSON data, and transform it into an R Data Frame.
# https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=yourkey
2.- Obtain Json File from API
Json
For this project I chose to use the Books API. In order to obtain the information we need to specify certain parameters to obtain specific data (as opposed to all the data available)
<- "https://api.nytimes.com/svc/books/v3/lists/{date}/{list}.json?api-key={api_key}" url_string
Parameters
The search parameters for are as follows:
<- Sys.getenv("NYT_TOKEN")
api_key <- "hardcover-nonfiction"
list <- "2020-04-01" date
Once parameters are set - we use JSONlite and HTTR to create a function and “GET” the information we requested in the parameters which is inside the the content variable.
<- fromJSON(rawToChar(GET(glue(url_string))$content))
raw.data <- raw.data$results$books book.data
For the purpose of this project we will work with the following variables.
<-
book.rank.df c("rank",
book.data[,"weeks_on_list",
"title",
"author",
"description",
"book_image")]
Final Result
kable(head(book.rank.df,5),caption = "NYT Hardcover Nonfiction Top 5")
rank | weeks_on_list | title | author | description | book_image |
---|---|---|---|---|---|
1 | 2 | UNTAMED | Glennon Doyle | The activist and public speaker describes her journey of listening to her inner voice. | https://storage.googleapis.com/du-prd/books/images/9781984801258.jpg |
2 | 4 | THE SPLENDID AND THE VILE | Erik Larson | An examination of the leadership of the prime minister Winston Churchill. | https://storage.googleapis.com/du-prd/books/images/9780385348713.jpg |
3 | 14 | THE MAMBA MENTALITY | Kobe Bryant | Various skills and techniques used on the court by the late Los Angeles Lakers player. | https://storage.googleapis.com/du-prd/books/images/9780374201234.jpg |
4 | 109 | EDUCATED | Tara Westover | The daughter of survivalists, who is kept out of school, educates herself enough to leave home for university. | https://storage.googleapis.com/du-prd/books/images/9780399590504.jpg |
5 | 7 | OPEN BOOK | Jessica Simpson with Kevin Carr O’Leary | The singer, actress and fashion designer discloses times of success, trauma and addiction. | https://storage.googleapis.com/du-prd/books/images/9780062899965.jpg |