In this assignment we are given The New York Times APIs web page to sign up for an API key in order to get access to their APIs Data. We will choose one of the APIs, construct an interface in R to read in the JSON data, and transform it into an R Dataframe.
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(jsonlite)
library(DT)
here we are extracting the data from NYT Book API by calling the Example Calls (2nd URL)
url <- "https://api.nytimes.com/svc/books/v3/reviews.json?author=Stephen+King&api-key=SOfRrxpoYgeFDX8eoGRqvq6lMEcqyEWH"
books_nyt <- fromJSON(url, flatten = TRUE)
books_nyt2 = data.frame(books_nyt$results)
head(books_nyt2)
## url
## 1 http://www.nytimes.com/2011/11/13/books/review/11-22-63-by-stephen-king-book-review.html
## 2 http://www.nytimes.com/2011/10/31/books/stephen-kings-11-23-63-review.html
## 3 http://www.nytimes.com/2004/01/04/books/the-quest-for-the-north-central-positronics.html
## 4 http://www.nytimes.com/1993/10/24/books/in-short-fiction-284093.html
## 5 http://www.nytimes.com/2001/11/04/books/books-in-brief-fiction-poetry-851302.html
## 6 http://www.nytimes.com/1990/05/13/books/armageddon-complete-and-uncut.html
## publication_dt byline book_title
## 1 2011-11-13 ERROL MORRIS 11/22/63
## 2 2011-10-31 JANET MASLIN 11/22/63
## 3 2004-01-04 ANDREW O'HEHIR Wolves of the Calla
## 4 1993-10-24 RICHARD E. NICHOLLS Nightmares and Dreamscapes
## 5 2001-11-04 MARY ELIZABETH WILLIAMS Black House
## 6 1990-05-13 ROBERT KIELY The Stand
## book_author
## 1 Stephen King
## 2 Stephen King
## 3 Stephen King
## 4 Stephen King
## 5 Stephen King
## 6 Stephen King
## summary
## 1 Stephen King’s time traveler tries to undo some painful history.
## 2 Stephen King’s latest novel, “11/22/63,” tells of a schoolteacher who travels back to 1958 to alter history, and falls in love as well.
## 3
## 4
## 5
## 6
## uuid
## 1 00000000-0000-0000-0000-000000000000
## 2 00000000-0000-0000-0000-000000000000
## 3 00000000-0000-0000-0000-000000000000
## 4 00000000-0000-0000-0000-000000000000
## 5 00000000-0000-0000-0000-000000000000
## 6 00000000-0000-0000-0000-000000000000
## uri
## 1 nyt://book/00000000-0000-0000-0000-000000000000
## 2 nyt://book/00000000-0000-0000-0000-000000000000
## 3 nyt://book/00000000-0000-0000-0000-000000000000
## 4 nyt://book/00000000-0000-0000-0000-000000000000
## 5 nyt://book/00000000-0000-0000-0000-000000000000
## 6 nyt://book/00000000-0000-0000-0000-000000000000
## isbn13
## 1 9780307951434, 9780606351461, 9781442344280, 9781442344303, 9781442391635, 9781444727326, 9781451627282, 9781451627299, 9781451627305, 9781451651645, 9781501120602, 9781594135590
## 2 9780307951434, 9780606351461, 9781442344280, 9781442344303, 9781442391635, 9781444727326, 9781451627282, 9781451627299, 9781451627305, 9781451651645, 9781501120602, 9781594135590
## 3 9781848941137
## 4 9781441615299
## 5 9780375504396
## 6 9781848940833
books_nyt3 <- select(books_nyt2, url, publication_dt, byline, book_title, book_author, summary)
colnames(books_nyt3) = c("Article URL", "Publication Date", "Byline", "Book Title", "Book Author", "Summary")
datatable(books_nyt3)
books_nyt3 <- books_nyt3[c("Book Title", "Book Author", "Publication Date", "Article URL", "Summary")]
datatable(books_nyt3)
In this assignment, we are able to learn:
How to create an API account on the New York Times Developer web site.
How to construct an interface in R to read the JSON data and transform it into a R DataFrame by using fromJSON() to Get a JSON file from NYT Archives API.
How to create a datafram using data.frame(), transform and rearrange the table by using select() and colnames() functions.