Introduction

The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis You’ll need to start by signing up for an API key. 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 packages
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
library(httr)
## Warning: package 'httr' was built under R version 4.2.3
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 4.2.3
library(dplyr)

Requesting data and turn it into a dataframe

library(jsonlite)

# Defining my New York Times API key
NYT_Key <- "ARP4fQUL5tLAXnY69qumoLCWcVD8G5SH"

# Constructing the URL for the API request with the API key as a parameter
url <- paste0("https://api.nytimes.com/svc/books/v3/reviews.json?author=Stephen+King&api-key=", NYT_Key)

# Making the API request and parse the JSON response
best_seller_types <- fromJSON(url)

# Accessing the relevant data within the JSON response
best_seller_types <- best_seller_types$results

# Checking the class of the data
class(best_seller_types)
## [1] "data.frame"
# Printing the first 5 rows of the 'best_seller_types' dataframe
print(head(best_seller_types, n = 5))
##                                                                                        url
## 1 http://www.nytimes.com/2011/11/13/books/review/11-22-63-by-stephen-king-book-review.html
## 2               http://www.nytimes.com/2011/10/31/books/stephen-kings-11-23-63-review.html
## 3 http://www.nytimes.com/2004/01/04/books/the-quest-for-the-north-central-positronics.html
## 4                     http://www.nytimes.com/1993/10/24/books/in-short-fiction-284093.html
## 5        http://www.nytimes.com/2001/11/04/books/books-in-brief-fiction-poetry-851302.html
##   publication_dt                  byline                 book_title
## 1     2011-11-13            ERROL MORRIS                   11/22/63
## 2     2011-10-31            JANET MASLIN                   11/22/63
## 3     2004-01-04          ANDREW O'HEHIR        Wolves of the Calla
## 4     1993-10-24     RICHARD E. NICHOLLS Nightmares and Dreamscapes
## 5     2001-11-04 MARY ELIZABETH WILLIAMS                Black House
##    book_author
## 1 Stephen King
## 2 Stephen King
## 3 Stephen King
## 4 Stephen King
## 5 Stephen King
##                                                                                                                                   summary
## 1                                                                        Stephen King’s time traveler tries to undo some painful history.
## 2 Stephen King’s latest novel, “11/22/63,” tells of a schoolteacher who travels back to 1958 to alter history, and falls in love as well.
## 3                                                                                                                                        
## 4                                                                                                                                        
## 5                                                                                                                                        
##                                   uuid
## 1 00000000-0000-0000-0000-000000000000
## 2 00000000-0000-0000-0000-000000000000
## 3 00000000-0000-0000-0000-000000000000
## 4 00000000-0000-0000-0000-000000000000
## 5 00000000-0000-0000-0000-000000000000
##                                               uri
## 1 nyt://book/00000000-0000-0000-0000-000000000000
## 2 nyt://book/00000000-0000-0000-0000-000000000000
## 3 nyt://book/00000000-0000-0000-0000-000000000000
## 4 nyt://book/00000000-0000-0000-0000-000000000000
## 5 nyt://book/00000000-0000-0000-0000-000000000000
##                                                                                                                                                                               isbn13
## 1 9780307951434, 9780606351461, 9781442344280, 9781442344303, 9781442391635, 9781444727326, 9781451627282, 9781451627299, 9781451627305, 9781451651645, 9781501120602, 9781594135590
## 2 9780307951434, 9780606351461, 9781442344280, 9781442344303, 9781442391635, 9781444727326, 9781451627282, 9781451627299, 9781451627305, 9781451651645, 9781501120602, 9781594135590
## 3                                                                                                                                                                      9781848941137
## 4                                                                                                                                                                      9781441615299
## 5                                                                                                                                                                      9780375504396

Dataframe

#Displaying data in a tabular format
knitr::kable(head(best_seller_types, n=5))
url publication_dt byline book_title book_author summary uuid uri isbn13
http://www.nytimes.com/2011/11/13/books/review/11-22-63-by-stephen-king-book-review.html 2011-11-13 ERROL MORRIS 11/22/63 Stephen King Stephen King’s time traveler tries to undo some painful history. 00000000-0000-0000-0000-000000000000 nyt://book/00000000-0000-0000-0000-000000000000 9780307951434, 9780606351461, 9781442344280, 9781442344303, 9781442391635, 9781444727326, 9781451627282, 9781451627299, 9781451627305, 9781451651645, 9781501120602, 9781594135590
http://www.nytimes.com/2011/10/31/books/stephen-kings-11-23-63-review.html 2011-10-31 JANET MASLIN 11/22/63 Stephen King Stephen King’s latest novel, “11/22/63,” tells of a schoolteacher who travels back to 1958 to alter history, and falls in love as well. 00000000-0000-0000-0000-000000000000 nyt://book/00000000-0000-0000-0000-000000000000 9780307951434, 9780606351461, 9781442344280, 9781442344303, 9781442391635, 9781444727326, 9781451627282, 9781451627299, 9781451627305, 9781451651645, 9781501120602, 9781594135590
http://www.nytimes.com/2004/01/04/books/the-quest-for-the-north-central-positronics.html 2004-01-04 ANDREW O’HEHIR Wolves of the Calla Stephen King 00000000-0000-0000-0000-000000000000 nyt://book/00000000-0000-0000-0000-000000000000 9781848941137
http://www.nytimes.com/1993/10/24/books/in-short-fiction-284093.html 1993-10-24 RICHARD E. NICHOLLS Nightmares and Dreamscapes Stephen King 00000000-0000-0000-0000-000000000000 nyt://book/00000000-0000-0000-0000-000000000000 9781441615299
http://www.nytimes.com/2001/11/04/books/books-in-brief-fiction-poetry-851302.html 2001-11-04 MARY ELIZABETH WILLIAMS Black House Stephen King 00000000-0000-0000-0000-000000000000 nyt://book/00000000-0000-0000-0000-000000000000 9780375504396
LS0tDQp0aXRsZTogIldlYiBBUElzIg0KYXV0aG9yOiAiTGF1cmEgUHVlYmxhIg0KZGF0ZTogImByIFN5cy5EYXRlKClgIg0Kb3V0cHV0OiBvcGVuaW50cm86OmxhYl9yZXBvcnQNCi0tLQ0KDQojIyMgSW50cm9kdWN0aW9uDQpUaGUgTmV3IFlvcmsgVGltZXMgd2ViIHNpdGUgcHJvdmlkZXMgYSByaWNoIHNldCBvZiBBUElzLCBhcyBkZXNjcmliZWQgaGVyZTogaHR0cHM6Ly9kZXZlbG9wZXIubnl0aW1lcy5jb20vYXBpcw0KWW914oCZbGwgbmVlZCB0byBzdGFydCBieSBzaWdua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