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)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
#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 |