After creating my dev account with the New York Times website I created an app with an api key. I made an API request using the GET function from the HTTR library. Printing the response gives us a status code of 200 which means it was successful.
r <- GET("https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=uUhvu5PYBLtlbDqzEsy9uo8DlG4O7iM3")
print(r)
## Response [https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=uUhvu5PYBLtlbDqzEsy9uo8DlG4O7iM3]
## Date: 2021-04-12 01:51
## Status: 200
## Content-Type: application/json; charset=UTF-8
## Size: 35.6 kB
We extract the text content from the response and then convert the json text into a dataframe using the function fromJSON(). Then we take the results out and convert that into a dataframe.
text_content <- content(r, "text")
json_content <- text_content %>% fromJSON
books <- data.frame(json_content$results$books)
glimpse(books)
## Rows: 15
## Columns: 26
## $ rank <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
## $ rank_last_week <int> 0, 1, 0, 2, 9, 7, 3, 5, 14, 0, 4, 12, 0, 11, 0
## $ weeks_on_list <int> 1, 9, 1, 18, 5, 23, 3, 5, 5, 1, 2, 44, 1, 5, 6
## $ asterisk <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## $ dagger <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## $ primary_isbn10 <chr> "059346527X", "1250178606", "0316499404", "05255…
## $ primary_isbn13 <chr> "9780593465271", "9781250178602", "9780316499408…
## $ publisher <chr> "Viking", "St. Martin's", "Little, Brown", "Viki…
## $ description <chr> "The poem read on President Joe Biden's Inaugura…
## $ price <chr> "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", …
## $ title <chr> "THE HILL WE CLIMB", "THE FOUR WINDS", "THE RED …
## $ author <chr> "Amanda Gorman", "Kristin Hannah", "James Patter…
## $ contributor <chr> "by Amanda Gorman", "by Kristin Hannah", "by Jam…
## $ contributor_note <chr> "", "", "", "", "", "", "", "", "", "", "", "", …
## $ book_image <chr> "https://storage.googleapis.com/du-prd/books/ima…
## $ book_image_width <int> 362, 329, 323, 331, 329, 331, 331, 329, 329, 327…
## $ book_image_height <int> 500, 500, 500, 500, 500, 500, 500, 500, 500, 500…
## $ amazon_product_url <chr> "https://www.amazon.com/dp/059346527X?tag=NYTBSR…
## $ age_group <chr> "", "", "", "", "", "", "", "", "", "", "", "", …
## $ book_review_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", …
## $ first_chapter_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", …
## $ sunday_review_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", …
## $ article_chapter_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", …
## $ isbns <list> [<data.frame[2 x 2]>, <data.frame[4 x 2]>, <dat…
## $ buy_links <list> [<data.frame[6 x 2]>, <data.frame[6 x 2]>, <dat…
## $ book_uri <chr> "nyt://book/a95afb91-235c-578c-847a-a98d0af7b6a0…
books %>%
ggplot(aes(x = reorder(title, rank) , y = rank)) +
geom_bar(stat="identity") +
labs(
x = "", y = "",
title = "Which hardback books are the most popular?"
) +
coord_flip()
books %>%
ggplot(aes(x = reorder(title, weeks_on_list) , y = weeks_on_list)) +
geom_bar(stat="identity") +
labs(
x = "", y = "",
title = "Which hardback books have been on the top 15 the longest?"
) +
coord_flip()