options(width = 100)
# This is a standard setup I include so that my working directory is set correctly whether I work on
# one of my windows or linux machines.
if (Sys.info()["sysname"] == "Windows") {
setwd("~/Masters/DATA607/Week9/Assignment")
} else {
setwd("~/Documents/Masters/DATA607/Week9/Assignment")
}
I set this function to return best sellers based on a date (to my son’s birthday!) in order to show how additional parameters work; however, if you remove the date_url parameter, you can get the current best sellers list.
Note, I know it does not appear like a lot of code but I actually refactored a lot of this after evaluating how JSON gets loaded into R. Specifically, it saved me a lot of lines of code when I realized I could drill into the structure using the $ sign (i.e. req$results$lists). Sorry the print output doesn’t look great but I wanted to show the intersting information.
Additionally note, I have hidden my personal api key in this assignment. You can find it from previous commits but since I just did this to show I knew how, I didn’t think wiping out old commits was really helpful.
suppressWarnings(suppressMessages(library(jsonlite)))
suppressWarnings(suppressMessages(library(tidyr)))
url <- "https://api.nytimes.com/svc/books/v3/lists/overview.json"
date_url <- "&published_date=2016-08-29"
req <- fromJSON(paste0(url, paste0("?api-key=", readLines("../../private/nytkey.txt")), date_url))
lists_df <- data.frame(req$results$lists)
books_df <- unnest(lists_df, books)[, c(2, 12, 20, 26, 27, 29, 31)]
books_df[books_df$list_name == "Combined Print and E-Book Fiction", ]
## list_name author
## 1 Combined Print and E-Book Fiction Paula Hawkins
## 2 Combined Print and E-Book Fiction Sandra Brown
## 3 Combined Print and E-Book Fiction Janet Evanovich and Phoef Sutton
## 4 Combined Print and E-Book Fiction Colson Whitehead
## 5 Combined Print and E-Book Fiction Liane Moriarty
## description
## 1 A psychological thriller set in the environs of London is full of complications and betrayals.
## 2 A hired killer and a woman he kidnapped join forces to elude the F.B.I. agents and others who are searching for her corrupt brother.
## 3 The first of a new series featuring Emerson Knight, an eccentric millionaire, and Riley Moon, an analyst at a mega-bank.
## 4 A teenage girl heads north on the network that helped slaves escape to freedom, envisioned in this novel as a secret system of actual tracks and tunnels.
## 5 Tense turning points for three couples at a backyard barbecue gone wrong.
## rank rank_last_week title weeks_on_list
## 1 1 2 THE GIRL ON THE TRAIN 78
## 2 2 0 STING 1
## 3 3 0 CURIOUS MINDS 1
## 4 4 3 THE UNDERGROUND RAILROAD 3
## 5 5 4 TRULY MADLY GUILTY 4
books_df[books_df$list_name == "Combined Print and E-Book Nonfiction", ]
## list_name author
## 6 Combined Print and E-Book Nonfiction Amy Schumer
## 7 Combined Print and E-Book Nonfiction JD Vance
## 8 Combined Print and E-Book Nonfiction Dick Morris and Eileen McGann
## 9 Combined Print and E-Book Nonfiction Paul Kalanithi
## 10 Combined Print and E-Book Nonfiction Daniel James Brown
## description
## 6 Humorous personal essays by the comedian, actor and writer.
## 7 A Yale Law School graduate looks at the struggles of the white working class through the story of his own childhood in the Rust Belt.
## 8 The political strategist offers a game plan for how to defeat Hillary Clinton.
## 9 A memoir by a physician who received a diagnosis of Stage IV lung cancer at the age of 36.
## 10 The University of Washington’s eight-oar crew and their quest for gold at the 1936 Berlin Olympics.
## rank rank_last_week title weeks_on_list
## 6 1 0 THE GIRL WITH THE LOWER BACK TATTOO 1
## 7 2 1 HILLBILLY ELEGY 4
## 8 3 5 ARMAGEDDON 5
## 9 4 2 WHEN BREATH BECOMES AIR 32
## 10 5 3 THE BOYS IN THE BOAT 117
unique(books_df[, 1])
## [1] "Combined Print and E-Book Fiction" "Combined Print and E-Book Nonfiction"
## [3] "Hardcover Fiction" "Hardcover Nonfiction"
## [5] "Trade Fiction Paperback" "Mass Market Paperback"
## [7] "Paperback Nonfiction" "E-Book Fiction"
## [9] "E-Book Nonfiction" "Advice How-To and Miscellaneous"
## [11] "Childrens Middle Grade E-Book" "Childrens Middle Grade Hardcover"
## [13] "Childrens Middle Grade Paperback" "Picture Books"
## [15] "Series Books" "Young Adult E-Book"
## [17] "Young Adult Hardcover" "Young Adult Paperback"
## [19] "Hardcover Graphic Books" "Paperback Graphic Books"
## [21] "Manga" "Animals"
## [23] "Business Books" "Celebrities"
## [25] "Crime and Punishment" "Culture"
## [27] "Education" "Espionage"
## [29] "Expeditions Disasters and Adventures" "Fashion Manners and Customs"
## [31] "Food and Fitness" "Games and Activities"
## [33] "Health" "Humor"
## [35] "Relationships" "Family"
## [37] "Hardcover Political Books" "Race and Civil Rights"
## [39] "Religion Spirituality and Faith" "Science"
## [41] "Sports" "Travel"