This assignment seeks to parse and load a NYTIMES API data set from JSON to a data frame. We use the Movie API to search for movie reviews about the Avengers, the recent Marvel Studios superheroes films.
library(tidyverse)
library(jsonlite)
library(knitr)
library(kableExtra)
source("NYTIMES_API_KEY.R") # By sourcing the private API key, we avoid displaying it directly
We use the jsonlite package to retrieve the New York Times API content
# Root for all URLs
NYT_URL = "https://api.nytimes.com/svc/movies/v2"
RELATIVE_URL = "/reviews/search.json?query=avengers"
URL = paste0(NYT_URL, RELATIVE_URL, "&api-key=", NYTIMES_API_KEY)
request <- fromJSON( URL, flatten = TRUE)
request$status
## [1] "OK"
request$num_results
## [1] 4
request$has_more
## [1] FALSE
Since there are been only 3 avenger movies produced from 2012 to 2018, according to Wikipedia [https://en.wikipedia.org/wiki/List_of_Marvel_Cinematic_Universe_films], we don’t expect too many search results.
The query returned 4 results which are the set of movie reviews. This seems reasonable and consistent with not returning more results. Lastly, the contents from jsonlite are stored into a component called results.
str(request$results)
## 'data.frame': 4 obs. of 16 variables:
## $ display_title : chr "Avengers: Infinity War" "Avengers: Age Of Ultron" "The Avengers" "The Avengers"
## $ mpaa_rating : chr "PG-13" "PG-13" "PG-13" "PG-13"
## $ critics_pick : int 0 0 0 0
## $ byline : chr "A.O. SCOTT" "MANOHLA DARGIS" "By A. O. SCOTT" "JANET MASLIN"
## $ headline : chr "‘Avengers: Infinity War’: It’s Marvel’s Universe. We Just Live in It." "Review: ‘Avengers: Age of Ultron’ Gets the Superband Back Together" "Superheroes, Super Battles, Super Egos" "Avengers, the (Movie)"
## $ summary_short : chr "Infinity? Come on. It’s not even three hours! But there is enough time to get a hunky-Chris showdown (Pratt vs."| __truncated__ "The new installment in the “Avengers” franchise adds characters, including the Scarlet Witch and Quicksilver, e"| __truncated__ "Joss Whedon’s long-awaited superhero opus “The Avengers” arrives with a big cast, frequent battles and abundant"| __truncated__ "British agents vs. villain with weather machine. Big-time flop."
## $ publication_date : chr "2018-04-24" "2015-04-30" "2012-05-03" "1998-08-15"
## $ opening_date : chr "2018-04-27" "2015-05-01" "2012-05-04" "1998-08-14"
## $ date_updated : chr "2018-05-24 16:44:02" "2017-11-02 04:18:21" "2017-11-02 04:18:15" "2017-11-02 04:17:55"
## $ link.type : chr "article" "article" "article" "article"
## $ link.url : chr "http://www.nytimes.com/2018/04/24/movies/avengers-infinity-war-review.html" "http://www.nytimes.com/2015/05/01/movies/review-avengers-age-of-ultron-gets-the-superband-back-together.html" "http://www.nytimes.com/2012/05/04/movies/robert-downey-jr-in-the-avengers-directed-by-joss-whedon.html" "http://www.nytimes.com/1998/08/15/movies/film-review-shh-they-re-trying-not-to-be-noticed.html"
## $ link.suggested_link_text: chr "Read the New York Times Review of Avengers: Infinity War" "Read the New York Times Review of Avengers: Age Of Ultron" "Read the New York Times Review of The Avengers" "Read the New York Times Review of The Avengers"
## $ multimedia.type : chr "mediumThreeByTwo210" "mediumThreeByTwo210" NA NA
## $ multimedia.src : chr "https://static01.nyt.com/images/2018/04/27/arts/27avengers/27avengers-mediumThreeByTwo210.jpg" "https://static01.nyt.com/images/2015/05/01/arts/avengers/avengers-mediumThreeByTwo210-v3.jpg" NA NA
## $ multimedia.width : int 210 210 NA NA
## $ multimedia.height : int 140 140 NA NA
Clearly, jsonlite has done most of the hard work of putting the content into a dataframe format. However, there are too many columns so we extract the salient ones.
request$results %>% select( display_title, opening_date, headline, summary_short, byline) %>%
arrange(opening_date) %>%
kable() %>%
kable_styling( )
| display_title | opening_date | headline | summary_short | byline |
|---|---|---|---|---|
| The Avengers | 1998-08-14 | Avengers, the (Movie) | British agents vs. villain with weather machine. Big-time flop. | JANET MASLIN |
| The Avengers | 2012-05-04 | Superheroes, Super Battles, Super Egos | Joss Whedon’s long-awaited superhero opus “The Avengers” arrives with a big cast, frequent battles and abundant banter among its characters. | By A. O. SCOTT |
| Avengers: Age Of Ultron | 2015-05-01 | Review: ‘Avengers: Age of Ultron’ Gets the Superband Back Together | The new installment in the “Avengers” franchise adds characters, including the Scarlet Witch and Quicksilver, even as its familiar heroes fight battles terrestrial and galactic. | MANOHLA DARGIS |
| Avengers: Infinity War | 2018-04-27 | ‘Avengers: Infinity War’: It’s Marvel’s Universe. We Just Live in It. | Infinity? Come on. It’s not even three hours! But there is enough time to get a hunky-Chris showdown (Pratt vs. Hemsworth vs. Evans). | A.O. SCOTT |
Lastly, we notice that the first Avengers movie is a British spy thriller, not one of the Marvel films. This one can be deleted as being irrelevant to the search.
request$results %>% select( display_title, opening_date, headline, summary_short, byline) %>%
arrange(opening_date) %>%
filter( row_number() > 1 ) %>%
kable(caption="Avenger Movie Reviews by the NYTimes (2012-2018)") %>%
kable_styling( bootstrap_options = c("hover", "striped" , "responsive", "bordered") )
| display_title | opening_date | headline | summary_short | byline |
|---|---|---|---|---|
| The Avengers | 2012-05-04 | Superheroes, Super Battles, Super Egos | Joss Whedon’s long-awaited superhero opus “The Avengers” arrives with a big cast, frequent battles and abundant banter among its characters. | By A. O. SCOTT |
| Avengers: Age Of Ultron | 2015-05-01 | Review: ‘Avengers: Age of Ultron’ Gets the Superband Back Together | The new installment in the “Avengers” franchise adds characters, including the Scarlet Witch and Quicksilver, even as its familiar heroes fight battles terrestrial and galactic. | MANOHLA DARGIS |
| Avengers: Infinity War | 2018-04-27 | ‘Avengers: Infinity War’: It’s Marvel’s Universe. We Just Live in It. | Infinity? Come on. It’s not even three hours! But there is enough time to get a hunky-Chris showdown (Pratt vs. Hemsworth vs. Evans). | A.O. SCOTT |
To conclude, we have shown how to access the NYTimes Movie API and query using the jsonlite library.