library(tidyverse)
library(httr)
library(stringr)
library(jsonlite)
library(knitr)
library(dplyr)
While access to the NY Times API is free, one must first first register here to retrieve an API key. This key is required for accessing the data.The book_key object stores the API key.The url object stores the URL for the NY Times books bestsellers API.
books_key <- "&api-key=c9ce3aad98444ca08c32a835c9a669d5"
url <- "https://api.nytimes.com/svc/books/v2/lists/best-sellers/history.json?"
using fromJSON() function in the jsonlite package to store the initial data to a data frame object.
response <- fromJSON(paste0(url, books_key)) # store data as data frame
bestsellers <- response$results # unmodified results
head(bestsellers)
## title
## 1 "I GIVE YOU MY BODY ..."
## 2 "MOST BLESSED OF THE PATRIARCHS"
## 3 #ASKGARYVEE
## 4 #GIRLBOSS
## 5 $100 STARTUP
## 6 $20 PER GALLON
## description
## 1 The author of the Outlander novels gives tips on writing sex scenes, drawing on examples from the books.
## 2 A character study that attempts to make sense of Jefferson’s contradictions.
## 3 The entrepreneur expands on subjects addressed on his Internet show, like marketing, management and social media.
## 4 An online fashion retailer traces her path to success.
## 5 How to build a profitable start up for $100 or less and be your own boss.
## 6
## contributor
## 1 by Diana Gabaldon
## 2 by Annette Gordon-Reed and Peter S. Onuf
## 3 by Gary Vaynerchuk
## 4 by Sophia Amoruso
## 5 by Chris Guillebeau
## 6 by Christopher Steiner
## author contributor_note price age_group
## 1 Diana Gabaldon 0
## 2 Annette Gordon-Reed and Peter S Onuf 0
## 3 Gary Vaynerchuk 0
## 4 Sophia Amoruso 0
## 5 Chris Guillebeau 23
## 6 Christopher Steiner 0
## publisher
## 1 Dell
## 2 Liveright
## 3 HarperCollins
## 4 Portfolio/Penguin/Putnam
## 5 Crown Business
## 6 Grand Central
## isbns
## 1 0399178570, 9780399178573
## 2 0871404427, 9780871404428
## 3 0062273124, 0062273132, 9780062273123, 9780062273130
## 4 039916927X, 1591847931, 9780399169274, 9781591847939
## 5 0307951529, 9780307951526
## 6 NULL
## ranks_history
## 1 0399178570, 9780399178573, 8, Advice How-To and Miscellaneous, Advice, How-To & Miscellaneous, 2016-09-04, 2016-08-20, 1, NA, 0, 0
## 2 0871404427, 9780871404428, 16, Hardcover Nonfiction, Hardcover Nonfiction, 2016-05-01, 2016-04-16, 1, NA, 1, 0
## 3 0062273124, 0062273124, 9780062273123, 9780062273123, 5, 6, Business Books, Advice How-To and Miscellaneous, Business, Advice, How-To & Miscellaneous, 2016-04-10, 2016-03-27, 2016-03-26, 2016-03-12, 0, 1, NA, NA, 0, 0, 1, 1
## 4 1591847931, 1591847931, 1591847931, 1591847931, 039916927X, 039916927X, 039916927X, 039916927X, 9781591847939, 9781591847939, 9781591847939, 9781591847939, 9780399169274, 9780399169274, 9780399169274, 9780399169274, 8, 9, 9, 8, 10, 8, 14, 13, Business Books, Business Books, Business Books, Business Books, Business Books, Business Books, Advice How-To and Miscellaneous, Advice How-To and Miscellaneous, Business, Business, Business, Business, Business, Business, Advice, How-To & Miscellaneous, Advice, How-To & Miscellaneous, 2016-03-13, 2016-01-17, 2015-12-13, 2015-11-15, 2014-11-09, 2014-10-12, 2014-09-21, 2014-09-14, 2016-02-27, 2016-01-02, 2015-11-28, 2015-10-31, 2014-10-25, 2014-09-27, 2014-09-06, 2014-08-30, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## 5 NULL
## 6 NULL
## reviews
## 1 , , ,
## 2 , , ,
## 3 , , ,
## 4 , , ,
## 5 , , ,
## 6 , , ,
colnames(bestsellers) # check names of columns
## [1] "title" "description" "contributor"
## [4] "author" "contributor_note" "price"
## [7] "age_group" "publisher" "isbns"
## [10] "ranks_history" "reviews"
bestsellers_1 <- subset(bestsellers[1:5, c("title", "author", "publisher","age_group")]) #modified results
kable(bestsellers_1) #printing only 4 columns in the table
| title | author | publisher | age_group |
|---|---|---|---|
| “I GIVE YOU MY BODY …” | Diana Gabaldon | Dell | |
| “MOST BLESSED OF THE PATRIARCHS” | Annette Gordon-Reed and Peter S Onuf | Liveright | |
| #ASKGARYVEE | Gary Vaynerchuk | HarperCollins | |
| #GIRLBOSS | Sophia Amoruso | Portfolio/Penguin/Putnam | |
| $100 STARTUP | Chris Guillebeau | Crown Business | |
| ##### Storing data in dataframe |
class(bestsellers)
## [1] "data.frame"
bestsellers$title[1]
## [1] "\"I GIVE YOU MY BODY ...\""
q<-"&q=Sports"
api.key <- "&api-key=c9ce3aad98444ca08c32a835c9a669d5"
url <- "https://api.nytimes.com/svc/search/v2/articlesearch.json?"
req <- fromJSON(paste0(url,q,api.key))
articles <- req$response$docs
colnames(articles)
## [1] "web_url" "snippet" "print_page"
## [4] "blog" "source" "multimedia"
## [7] "headline" "keywords" "pub_date"
## [10] "document_type" "new_desk" "byline"
## [13] "type_of_material" "_id" "word_count"
## [16] "score" "uri"
df <- data.frame(headline = req$response$docs$headline$main,pub_date = req$response$docs$pub_date,web_url = req$response$docs$web_url,snippet = req$response$docs$snippet)
kable(df)
| headline | pub_date | web_url | snippet |
|---|---|---|---|
| Tell Us 5 Things About Your Book: Inside the Fevered Minds of Sports Fans | 2018-02-18T16:00:04+0000 | https://www.nytimes.com/2018/02/18/books/superfans-george-dohrmann.html | To write “Superfans,” George Dohrmann spoke to everyday fans, academics and scientists about what it is that drives our vicarious competitive mania. |
| What Americans Really Think About Sports Betting | 2018-03-31T03:45:49+0000 | https://www.nytimes.com/2018/03/30/opinion/sports-betting-final-four.html | Our attitudes are finally catching up to gambling’s long history with sports — and a billion-dollar industry. |
| The College Sports Tax Dodge | 2017-12-28T19:45:25+0000 | https://www.nytimes.com/2017/12/28/sunday-review/college-sports-tax-dodge.html | A loophole written when collegiate athletics were a trivial business allows billions of dollars in revenue to go untaxed. |
| Why Russia Tried to Cheat Its Way to Glory | 2017-12-08T17:53:47+0000 | https://www.nytimes.com/2017/12/08/sunday-review/how-russia-cheats.html | Russia’s election meddling and its Olympic doping are at the heart of President Putin’s effort to recapture his country’s past. |
| Sports | NA | https://www.nytimes.com/pages/sports/sports-email/index.html | |
| Officiating (Sports) | NA | https://topics.nytimes.com/top/reference/timestopics/subjects/o/officiating_sports/index.html | News about Sports Officiating, including commentary and archival articles published in The New York Times. |
| Officiating (Sports) | NA | https://topics.nytimes.com/topic/subject/officiating-sports | News about Sports Officiating, including commentary and archival articles published in The New York Times. |
| At the Australian Open, Too Hot to Be Good | 2018-01-20T03:03:34+0000 | https://www.nytimes.com/2018/01/19/opinion/tennis-australian-open-heat-temperature.html | The scorching temperatures at the year’s first tennis major are hurting players and cheating spectators. |
| In This Sports Gender Gap, Men Fall Short | 2017-09-16T02:30:32+0000 | https://www.nytimes.com/2017/09/15/opinion/sports-gender-gap-serena.html | We’d rather our male athletes not have emotional lives, but if they have to have them, we’d rather not know about it. |
| Bob Beattie, 85, Olympic Ski Coach and ABC Sports Analyst, Dies | 2018-04-03T21:31:05+0000 | https://www.nytimes.com/2018/04/03/obituaries/bob-beattie-85-olympic-ski-coach-and-abc-sports-analyst-dies.html | A commentator brought an intense knowledge of Olympic and World Cup skiing to the early days of “Wide World of Sports.” |
Storing data frame
class(df)
## [1] "data.frame"
df$headline[1]
## [1] Tell Us 5 Things About Your Book: Inside the Fevered Minds of Sports Fans
## 9 Levels: At the Australian Open, Too Hot to Be Good ...