library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(httr2)
## Warning: package 'httr2' was built under R version 4.4.1
library(jsonlite)
First, I signed up for a developer account and key for the New York
Times API. Working with Books data, I set up a request using the
httr
package to fetch the latest list of the most popular
fiction e-books. My API key is set as a variable in the local
environment, in the user-level .Renviron
file. I also added
a very minimal error message; this is standard for HTTP requests.
list_url <- "https://api.nytimes.com/svc/books/v3/lists/current"
ebook_path <- "/e-book-fiction.json"
NYT_API_KEY <- Sys.getenv("NYT_API_KEY")
error_msg <- function(resp) {
return("Unable to complete request.")
}
resp <- request(list_url) |>
req_url_path_append(ebook_path) |>
req_url_query(`api-key` = NYT_API_KEY) |>
req_error(body = error_msg) |>
req_perform()
The above code block returns an HTTP response when successful. Next,
I converted the response body into a string that could then be parsed
into JSON, using the jsonlite
package. At this point, the
(very wide) raw dataframe books
is available for
extraction.
data_str <- resp_body_string(resp)
ebooks <- fromJSON(data_str)
ebooks <- ebooks$results$books
knitr::kable(ebooks[, 1:9])
rank | rank_last_week | weeks_on_list | asterisk | dagger | primary_isbn10 | primary_isbn13 | publisher | description |
---|---|---|---|---|---|---|---|---|
1 | 0 | 1 | 0 | 0 | None | A00B01MT5HMRV | Lauren Blakely | A man shares a cramped apartment with his friend’s fetching sister. |
2 | 0 | 2 | 0 | 0 | 0440337976 | 9780440337973 | Dial | After World War II, a journalist travels to the island of Guernsey to meet residents who resisted the Nazi occupation. Originally published in 2008. |
3 | 4 | 12 | 0 | 0 | 0385541201 | 9780385541206 | Doubleday | A whistleblower alerts a Florida investigator to judicial corruption involving the Mob and Indian casinos. |
4 | 0 | 1 | 0 | 0 | None | 9781101984772 | Dutton | Pike Logan, a member of a secret counterterrorist unit called the Taskforce, investigates a Saudi-backed Moroccan terrorist cell. |
5 | 7 | 9 | 0 | 0 | 034554496X | 9780345544964 | Ballantine | A medical crisis entangles a black nurse, a white supremacist father and a white lawyer. |
6 | 0 | 1 | 0 | 0 | None | A00B01MF62CN8 | Stoker Aces Production | A fireman must keep his overprotective nature in check while pursuing an epileptic prey to her suspiciously obsessive boss. |
7 | 9 | 3 | 0 | 0 | None | 9781476738031 | Atria | An angry old curmudgeon gets new neighbors, and things are about to change for all of them. |
8 | 2 | 2 | 0 | 0 | 0425285359 | 9780425285350 | Delacorte | The beautiful mistress of a Russian oligarch falls in love with an artist and yearns for freedom. |
9 | 0 | 1 | 0 | 0 | 9780698193000 | Berkley | Spirits invade the life of a Charleston realtor. | |
10 | 0 | 8 | 0 | 0 | 1455586498 | 9781455586493 | Grand Central | John Puller, a special agent with the Army, searches for the truth about his mother, who disappeared 30 years ago. |
11 | 0 | 0 | 0 | 0 | None | 9780399574184 | Putnam | The New York lawyer Stone Barrington faces danger when he finds himself in possession of a retired C.I.A. agent’s explosive memoir. |
12 | 0 | 0 | 0 | 0 | 031640716X | 9780316407168 | Little, Brown | Detective Alex Cross and his wife, Bree, team up to catch a killer causing chaos in Washington, D.C. |
13 | 0 | 0 | 0 | 0 | 1250105617 | 9781250105615 | Flatiron | |
14 | 0 | 0 | 0 | 0 | 1429960272 | 9781429960274 | Forge | A canine narrator undergoes a series of reincarnations. |
15 | 0 | 0 | 0 | 0 | 0385538928 | 9780385538923 | Doubleday | The daughters of a Vermont woman who disappeared from her home in the middle of the night try to understand what happened. |
I decided to subset the most relevant columns and fill in any empty or “None” strings with NA values for ease in future analysis. This is the final tidy dataframe of the Top 15 Fiction E-Books of this week:
ebooks <- ebooks |>
select(rank, weeks_on_list, primary_isbn13, title, author, publisher) |>
mutate(across(everything(), ~ ifelse(. %in% c("", "None"), NA, .)))
knitr::kable(ebooks)
rank | weeks_on_list | primary_isbn13 | title | author | publisher |
---|---|---|---|---|---|
1 | 1 | A00B01MT5HMRV | FULL PACKAGE | Lauren Blakely | Lauren Blakely |
2 | 2 | 9780440337973 | THE GUERNSEY LITERARY AND POTATO PEEL PIE SOCIETY | Mary Ann Shaffer and Annie Barrows | Dial |
3 | 12 | 9780385541206 | THE WHISTLER | John Grisham | Doubleday |
4 | 1 | 9781101984772 | RING OF FIRE | Brad Taylor | Dutton |
5 | 9 | 9780345544964 | SMALL GREAT THINGS | Jodi Picoult | Ballantine |
6 | 1 | A00B01MF62CN8 | SHELTER FOR ADELINE | Susan Stoker | Stoker Aces Production |
7 | 3 | 9781476738031 | A MAN CALLED OVE | Fredrik Backman | Atria |
8 | 2 | 9780425285350 | THE MISTRESS | Danielle Steel | Delacorte |
9 | 1 | 9780698193000 | THE GUESTS ON SOUTH BATTERY | Karen White | Berkley |
10 | 8 | 9781455586493 | NO MAN’S LAND | David Baldacci | Grand Central |
11 | 0 | 9780399574184 | BELOW THE BELT | Stuart Woods | Putnam |
12 | 0 | 9780316407168 | CROSS THE LINE | James Patterson | Little, Brown |
13 | 0 | 9781250105615 | THE DRY | Jane Harper | Flatiron |
14 | 0 | 9781429960274 | A DOG’S PURPOSE | W Bruce Cameron | Forge |
15 | 0 | 9780385538923 | THE SLEEPWALKER | Chris Bohjalian | Doubleday |