library(jsonlite)
library(httr)
library(DT)
library(dplyr)
The assignment requires us to sign up for a NYTimes API key. We are then asked to choose one of the New York Times APIS and construct an interface in R to read in the JSON data and transform it into an R dataframe.
We signed up for an API key at NYTimesby creating an app. We then set up the API key as a global variable so we may call it later from within R using the Sys.getenv("apikey)
Sys.setenv(apikey="&api-key=*******************")
Sys.getenv("apikey")
WE wrote a query to give us a list of movie reviews for all movies that have the word alien in it.We then constructed the apiurl by pasting together the url with the query and the api key.
url<-"https://api.nytimes.com/svc/movies/v2/reviews/search.json?"
query<-"query=alien"
apiurl<-paste0(url, query, Sys.getenv("apikey"))
We use the GET command from httr library to get the data from New York Times. We find that it is a list of 10 with nested lists inside. We also check the status code to make sure the code is 200. A status code of 200 with the GET command means that the resource has been fetched and has been transmitted to the message body.
response <- GET(apiurl, accept_json())
response$status_code
## [1] 200
Now that we have tested our connection, we will import the content using fromJSON from JSONlite library.We will flatten the data to convert the results to a 2 dimensional structure.We are displaying the whole dataframe as required by the assignment.
json<-fromJSON(apiurl, flatten=TRUE)
movieresults<-as.data.frame(json$results)
colnames(movieresults)
## [1] "display_title" "mpaa_rating"
## [3] "critics_pick" "byline"
## [5] "headline" "summary_short"
## [7] "publication_date" "opening_date"
## [9] "date_updated" "link.type"
## [11] "link.url" "link.suggested_link_text"
## [13] "multimedia.type" "multimedia.src"
## [15] "multimedia.width" "multimedia.height"
movieresults
## display_title mpaa_rating critics_pick
## 1 Memory: The Origins of Alien 0
## 2 Alien: Covenant R 0
## 3 Alien Outpost Not Rated 0
## 4 Alien Abduction Not Rated 0
## 5 Codependent Lesbian Space Alien Seeks Same 0
## 6 Cowboys & Aliens PG-13 0
## 7 Aliens in the Attic PG 0
## 8 Alien Trespass PG 0
## 9 Monsters vs. Aliens PG 0
## 10 How to Lose Friends & Alienate People R 0
## 11 Aliens vs. Predator: Requiem R 0
## 12 Alien vs. Predator 0
## 13 Mutant Aliens R 0
## 14 Alien Resurrection R 0
## 15 Three Lives and Only One Death 0
## 16 The Hairdresser's Husband R 0
## 17 Alien3 R 0
## 18 Resident Alien 0
## 19 Rorret 0
## 20 My Stepmother Is An Alien PG-13 0
## byline
## 1 BEN KENIGSBERG
## 2 A.O. SCOTT
## 3 JEANNETTE CATSOULIS
## 4 JEANNETTE CATSOULIS
## 5 JEANNETTE CATSOULIS
## 6 MANOHLA DARGIS
## 7 MIKE HALE
## 8 JEANNETTE CATSOULIS
## 9 A. O. SCOTT
## 10 MANOHLA DARGIS
## 11 NEIL GENZLINGER
## 12 Dave Kehr
## 13 Dave Kehr
## 14 Janet Maslin
## 15 STEPHEN HOLDEN
## 16 JANET MASLIN
## 17 Vincent Canby
## 18 Janet Maslin
## 19 VINCENT CANBY
## 20 Janet Maslin
## headline
## 1 ‘Memory: The Origins of Alien’ Review: Extras on an Extraterrestrial
## 2 Review: ‘Alien: Covenant’ Stays on Brand With Its Terror
## 3 Embedded With the E. T. Fighters
## 4 A Family Finds There’s Something Out There
## 5 Lesbian Alien Looks for Love Light Years Away
## 6 Extraterrestrials Land at the O.K. Corral
## 7 Earth in Peril, Children to the Rescue
## 8 Monsters, Aliens and Nostalgia
## 9 3-D Fun With a Beefed-Up Bride and a Dumb Blob Named B.O.B.
## 10 A Guide to Burning Your Bridges
## 11 Aliens and Predators Still Can’t Seem to Get Along
## 12 It's an Underground Monster World Series
## 13 Mutant Aliens (Movie)
## 14 Alien Resurrection (Movie)
## 15 Three Lives and Only One Death (Movie)
## 16 HAIRDRESSER'S HUSBAND, THE (MOVIE)
## 17 ALIEN 3 (MOVIE)
## 18 RESIDENT ALIEN (MOVIE)
## 19 RORRET (MOVIE)
## 20 MY STEPMOTHER IS AN ALIEN (MOVIE)
## summary_short
## 1 This documentary about Ridley Scott’s 1979 science-fiction classic often plays like a conventional making-of doc.
## 2 The new entry in Ridley Scott’s long-lived franchise is committed to the canny management of expectations, but that doesn’t diminish its creepiness.
## 3 The low-budget “Alien Outpost” is shaped like a boots-on-the-ground documentary but is more akin to a futuristic war movie.
## 4 A family has an adventure in “Alien Abduction,” a sci-fi film inspired by so-called sightings in the Brown Mountain area of North Carolina.
## 5 A lonely Manhattanite and an exiled extraterrestrial find interspecies contentment in “Codependent Lesbian Space Alien Seeks Same.”
## 6 In “Cowboys & Aliens,” Daniel Craig rides into the New Mexico Territory and wrangles extraterrestrials that look like cousins of the monsters from the “Alien” films.
## 7 In “Aliens in the Attic,” basically a tweener cable movie on steroids, a group of green critters from space battle a group of youngstes as the fate of the planet hangs in the balance.
## 8 “Alien Trespass” is a charmingly sentimental but ultimately pointless homage to the sci-fi classics of yesteryear.
## 9 “Monsters vs. Aliens” cheerfully converts two major sources of cinematic terror into wacky, goofy, familiar figures.
## 10 The crushingly unfunny and slopped-together “How to Lose Friends & Alienate People” has neither the ambition nor the intelligence to do justice to its source material.
## 11 The second “Aliens vs. Predator” film is, like its predecessor, one very dark movie. Not psychologically dark; dark dark, as in not very generously lighted.
## 12 Set in a Jules Verne world beneath the earth's surface in an incalculably ancient pyramid buried 2,000 feet beneath the Antarctic ice cap, Paul W.H. Anderson's film imagines an apocalyptic conflict between two of 20th-Century Fox's franchise monsters, on loan from "Alien" (1979) and "Predator" (1986). The humans don't have much to do in "Alien vs. Predator" but put the plot in motion and stand back to watch it work, a precarious position that eventually claims most of them. Though Mr. Anderson does have an idea for creating a rooting interest, it's too far-fetched even for fantasy, and the film turns into a cold but occasionally impressive special-effects extravaganza. — Dave Kehr
## 13 It is hard not to admire Bill Plympton, the maverick animator whose distinctive, pencil-sketch style has graced a host of short films, commercials, MTV interstitials and three full-length animated features, including his latest work, "Mutant Aliens." Mr. Plympton is unquestionably a brilliant draftsman, and within the severe limitations of his small budgets (most of his films have been self-financed) he is a capable, often inventive animator. But he has yet to find a steady narrative voice to match his drawing skills, and "Mutant Aliens," like his previous features "The Tune" (1992) and "I Married a Strange Person" (1997) doesn't hang together. — Dave Kehr\r\n
## 14 Slime time again, with heroine now an alien's mom. Freakish, macabre sequel.
## 15
## 16 Man's lifelong joy in having hair cut by women. There are limits to how deep you can go with this.
## 17 Sole woman on prison planet. Dizzying.
## 18
## 19
## 20 Amusingly eccentric Kim and O.K. story stymied by contrived silliness.
## publication_date opening_date date_updated link.type
## 1 2019-10-03 2019-10-04 2019-10-11 16:44:02 article
## 2 2017-05-17 2017-05-19 2017-11-02 04:18:27 article
## 3 2015-01-29 <NA> 2017-11-02 04:16:39 article
## 4 2014-04-03 <NA> 2017-11-02 04:16:40 article
## 5 2012-01-05 2012-01-06 2017-11-02 04:18:14 article
## 6 2011-07-28 2011-07-29 2017-11-02 04:18:13 article
## 7 2009-07-31 2009-07-31 2017-11-02 04:18:09 article
## 8 2009-04-02 <NA> 2017-11-02 04:16:29 article
## 9 2009-03-26 2009-03-27 2017-11-02 04:18:09 article
## 10 2008-10-02 2008-10-03 2017-11-02 04:18:08 article
## 11 2007-12-26 2007-12-25 2017-11-02 04:18:07 article
## 12 2004-08-14 2004-08-13 2017-11-02 04:18:04 article
## 13 2002-04-19 <NA> 2017-11-02 04:16:20 article
## 14 1997-11-26 1997-11-26 2017-11-02 04:17:54 article
## 15 1996-10-09 1996-10-11 2017-11-02 04:17:51 article
## 16 1992-06-19 <NA> 2017-11-02 04:16:10 article
## 17 1992-05-22 1992-05-22 2017-11-02 04:17:44 article
## 18 1991-10-18 <NA> 2017-11-02 04:16:11 article
## 19 1989-02-08 <NA> 2017-11-02 04:16:09 article
## 20 1988-12-09 1988-12-09 2017-11-02 04:17:39 article
## link.url
## 1 http://www.nytimes.com/2019/10/03/movies/memory-the-origins-of-alien-review.html
## 2 http://www.nytimes.com/2017/05/17/movies/alien-covenant-review-ridley-scott.html
## 3 http://www.nytimes.com/2015/01/30/movies/alien-outpost-directed-by-jabbar-raisani.html
## 4 http://www.nytimes.com/2014/04/04/movies/alien-abduction-a-thriller-directed-by-matty-beckerman.html
## 5 http://www.nytimes.com/2012/01/06/movies/codependent-lesbian-space-alien-seeks-same-from-madeleine-olnek.html
## 6 http://www.nytimes.com/2011/07/29/movies/cowboys-aliens-with-daniel-craig-review.html
## 7 http://www.nytimes.com/2009/08/01/movies/01alien.html
## 8 http://www.nytimes.com/2009/04/03/movies/03tres.html
## 9 http://www.nytimes.com/2009/03/27/movies/27mons.html
## 10 http://www.nytimes.com/2008/10/03/movies/03lose.html
## 11 http://www.nytimes.com/2007/12/26/movies/26alie.html
## 12 http://www.nytimes.com/2004/08/14/movies/film-review-it-s-an-underground-monster-world-series.html
## 13 http://www.nytimes.com/2002/04/19/movies/film-in-review-mutant-aliens.html
## 14 http://www.nytimes.com/1997/11/26/movies/film-review-ripley-believe-it-or-not-has-a-secret-and-it-s-not-pretty.html
## 15 http://www.nytimes.com/1996/10/09/movies/four-men-in-one-body-and-it-s-mastroianni-s.html
## 16 http://www.nytimes.com/1992/06/19/movies/reviews-film-if-a-haircut-s-erotic-marry-the-barber.html
## 17 http://www.nytimes.com/1992/05/22/movies/review-film-alien-3-hal-if-you-re-still-there-here-s-computer-friendly-sequel.html
## 18 http://www.nytimes.com/1991/10/18/movies/reviews-film-everybody-believed-he-had-to-be-somebody.html
## 19 http://www.nytimes.com/1989/02/08/movies/review-film-visions-of-a-psychopath.html
## 20 http://www.nytimes.com/1988/12/09/movies/review-film-an-alien-who-s-cute-and-curvy.html
## link.suggested_link_text
## 1 Read the New York Times Review of Memory: The Origins of Alien
## 2 Read the New York Times Review of Alien: Covenant
## 3 Read the New York Times Review of Alien Outpost
## 4 Read the New York Times Review of Alien Abduction
## 5 Read the New York Times Review of Codependent Lesbian Space Alien Seeks Same
## 6 Read the New York Times Review of Cowboys & Aliens
## 7 Read the New York Times Review of Aliens in the Attic
## 8 Read the New York Times Review of Alien Trespass
## 9 Read the New York Times Review of Monsters vs. Aliens
## 10 Read the New York Times Review of How to Lose Friends & Alienate People
## 11 Read the New York Times Review of Aliens vs. Predator: Requiem
## 12 Read the New York Times Review of Alien vs. Predator
## 13 Read the New York Times Review of Mutant Aliens
## 14 Read the New York Times Review of Alien Resurrection
## 15 Read the New York Times Review of Three Lives and Only One Death
## 16 Read the New York Times Review of The Hairdresser's Husband
## 17 Read the New York Times Review of Alien3
## 18 Read the New York Times Review of Resident Alien
## 19 Read the New York Times Review of Rorret
## 20 Read the New York Times Review of My Stepmother Is An Alien
## multimedia.type
## 1 mediumThreeByTwo210
## 2 mediumThreeByTwo210
## 3 mediumThreeByTwo210
## 4 mediumThreeByTwo210
## 5 <NA>
## 6 <NA>
## 7 <NA>
## 8 <NA>
## 9 <NA>
## 10 <NA>
## 11 <NA>
## 12 <NA>
## 13 <NA>
## 14 <NA>
## 15 <NA>
## 16 <NA>
## 17 <NA>
## 18 <NA>
## 19 <NA>
## 20 <NA>
## multimedia.src
## 1 https://static01.nyt.com/images/2019/10/04/arts/03memory-art/03memory-art-mediumThreeByTwo210.jpg
## 2 https://static01.nyt.com/images/2017/05/19/arts/19ALIEN1/19ALIEN1-mediumThreeByTwo210-v2.jpg
## 3 https://static01.nyt.com/images/2015/01/30/arts/30ALIEN/ALIEN-mediumThreeByTwo210.jpg
## 4 https://static01.nyt.com/images/2014/04/04/arts/09RDP_ALIEN_SPAN/09RDP_ALIEN_SPAN-mediumThreeByTwo210.jpg
## 5 <NA>
## 6 <NA>
## 7 <NA>
## 8 <NA>
## 9 <NA>
## 10 <NA>
## 11 <NA>
## 12 <NA>
## 13 <NA>
## 14 <NA>
## 15 <NA>
## 16 <NA>
## 17 <NA>
## 18 <NA>
## 19 <NA>
## 20 <NA>
## multimedia.width multimedia.height
## 1 210 140
## 2 210 140
## 3 210 140
## 4 210 140
## 5 NA NA
## 6 NA NA
## 7 NA NA
## 8 NA NA
## 9 NA NA
## 10 NA NA
## 11 NA NA
## 12 NA NA
## 13 NA NA
## 14 NA NA
## 15 NA NA
## 16 NA NA
## 17 NA NA
## 18 NA NA
## 19 NA NA
## 20 NA NA
movieresults1<-movieresults %>%
select(display_title, mpaa_rating, byline, headline, summary_short)
datatable(movieresults1)