Introduction

In this assignment we will demonstrate how to fetch and analyze books data from the New York Times Books API. The list we’re interested in is “Hardcover Fiction,” and we will transform the JSON response into an R data frame.

Fetching Data from the API

# Function to fetch and transform NY Times Books data
get_nyt_books_data <- function(api_key, list_name = "hardcover-fiction") {
  # Construct the URL
  url <- paste0("https://api.nytimes.com/svc/books/v3/lists.json?list=", list_name, "&api-key=", api_key)
  
  # Make the GET request
  response <- GET(url, accept("application/json"))
  
if (status_code(response) == 200) {
  json_data <- content(response, as = "text", encoding = "UTF-8")
  data <- fromJSON(json_data, flatten = TRUE)
  
  # Extract book details into a separate data frame
books_details_df <- data$results %>%
  mutate(book_details = map(book_details, ~ .x %>% 
                              as.data.frame() %>% 
                              select(title, author))) %>%
  unnest(book_details)

#select and work with columns directly
books_summary <- books_details_df %>%
  select(rank, title, author, weeks_on_list)

print(books_summary)
} else {
  books_df <- NULL
  print("Failed to fetch data from the API.")
}
}


api_key <- "gYMvXAooRmMGPCBIAj8ADZAIT0pBzW97"
books_data <- get_nyt_books_data(api_key)
## # A tibble: 15 × 4
##     rank title                      author                     weeks_on_list
##    <int> <chr>                      <chr>                              <int>
##  1     1 IN TOO DEEP                Lee Child and Andrew Child             1
##  2     2 BLOODGUARD                 Cecy Robson                            1
##  3     3 THE WAITING                Michael Connelly                       2
##  4     4 COUNTING MIRACLES          Nicholas Sparks                        5
##  5     5 THE WOMEN                  Kristin Hannah                        38
##  6     6 IRON FLAME                 Rebecca Yarros                        51
##  7     7 ABSOLUTION                 Jeff VanderMeer                        1
##  8     8 INTERMEZZO                 Sally Rooney                           5
##  9     9 HERE ONE MOMENT            Liane Moriarty                         7
## 10    10 MEMORIALS                  Richard Chizmar                        1
## 11    11 THE GOD OF THE WOODS       Liz Moore                             17
## 12    12 THE STARS ARE DYING        Chloe C. Peñaranda                     3
## 13    13 JAMES                      Percival Everett                      17
## 14    14 FOURTH WING                Rebecca Yarros                        74
## 15    15 ALL THE COLORS OF THE DARK Chris Whitaker                        15

Displaying the Data

The data fetched from the New York Times Books API has been converted into a data frame and displayed below

head(books_data)
## # A tibble: 6 × 4
##    rank title             author                     weeks_on_list
##   <int> <chr>             <chr>                              <int>
## 1     1 IN TOO DEEP       Lee Child and Andrew Child             1
## 2     2 BLOODGUARD        Cecy Robson                            1
## 3     3 THE WAITING       Michael Connelly                       2
## 4     4 COUNTING MIRACLES Nicholas Sparks                        5
## 5     5 THE WOMEN         Kristin Hannah                        38
## 6     6 IRON FLAME        Rebecca Yarros                        51

Analysis

Now that the data is loaded, lets perform some analysis.

# Summarize the data
summary(books_data)
##       rank         title              author          weeks_on_list  
##  Min.   : 1.0   Length:15          Length:15          Min.   : 1.00  
##  1st Qu.: 4.5   Class :character   Class :character   1st Qu.: 1.50  
##  Median : 8.0   Mode  :character   Mode  :character   Median : 5.00  
##  Mean   : 8.0                                         Mean   :15.87  
##  3rd Qu.:11.5                                         3rd Qu.:17.00  
##  Max.   :15.0                                         Max.   :74.00
# Check unique book titles
unique(books_data$title)
##  [1] "IN TOO DEEP"                "BLOODGUARD"                
##  [3] "THE WAITING"                "COUNTING MIRACLES"         
##  [5] "THE WOMEN"                  "IRON FLAME"                
##  [7] "ABSOLUTION"                 "INTERMEZZO"                
##  [9] "HERE ONE MOMENT"            "MEMORIALS"                 
## [11] "THE GOD OF THE WOODS"       "THE STARS ARE DYING"       
## [13] "JAMES"                      "FOURTH WING"               
## [15] "ALL THE COLORS OF THE DARK"

Conclusion

This assignment shows how to interface with an external API, parse JSON data, and transform it into an R data frame for further analysis. The data can be used for various visualizations and analyses in R.