Assignment – Web APIs

The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs You’ll need to start by signing up for an API key.

Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it to an R dataframe.

*I chose to get a key for the Movie Ratings API.

Load all the needed libararies

gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 495542 26.5     940480 50.3   750400 40.1
## Vcells 935685  7.2    1650153 12.6  1187628  9.1
rm(list=ls())
library(jsonlite)
library(tidyr)
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(knitr)
api_key<-"e574a2335cc54ceeaf5a074a336e76bc"
url <- "http://api.nytimes.com/svc/movies/v2/reviews/search.json?api_key="
api_url= paste(url,api_key,sep="")
api_url
## [1] "http://api.nytimes.com/svc/movies/v2/reviews/search.json?api_key=e574a2335cc54ceeaf5a074a336e76bc"
#Read JSON using API key
movies<- fromJSON(api_url, flatten = TRUE)

#Convert JSON into data.frame
movies<- data.frame(movies)
#review the column names 
colnames(movies)
##  [1] "status"                           "copyright"                       
##  [3] "has_more"                         "num_results"                     
##  [5] "results.display_title"            "results.mpaa_rating"             
##  [7] "results.critics_pick"             "results.byline"                  
##  [9] "results.headline"                 "results.summary_short"           
## [11] "results.publication_date"         "results.opening_date"            
## [13] "results.date_updated"             "results.link.type"               
## [15] "results.link.url"                 "results.link.suggested_link_text"
## [17] "results.multimedia.type"          "results.multimedia.src"          
## [19] "results.multimedia.width"         "results.multimedia.height"
#Choose only the relevant columns 
movies_updated<- movies[c(4,5,6,7,8,9,10,11,12,17)]
head(movies_updated)
##   num_results  results.display_title results.mpaa_rating
## 1          20 Tyler Perry's Acrimony                   R
## 2          20           Wilde Salomé                   R
## 3          20                 Salomé                   R
## 4          20      Personal Problems                    
## 5          20        Love After Love                    
## 6          20             Outside In                    
##   results.critics_pick      results.byline
## 1                    0      BEN KENIGSBERG
## 2                    1         GLENN KENNY
## 3                    1         GLENN KENNY
## 4                    1         GLENN KENNY
## 5                    1 JEANNETTE CATSOULIS
## 6                    1 JEANNETTE CATSOULIS
##                                                                results.headline
## 1      Review: In ‘Acrimony,’ Taraji P. Henson Endures Marriage and Script Woes
## 2                             Review: Al Pacino’s Journey With Wilde’s ‘Salomé’
## 3                             Review: Al Pacino’s Journey With Wilde’s ‘Salomé’
## 4 Review: ‘Personal Problems,’ a Look at African-American Life in 1980 New York
## 5           Review: In ‘Love After Love,’ an Unflinching Look at Extended Grief
## 6                                 Review: On Parole and in Love in ‘Outside In’
##                                                                                                                        results.summary_short
## 1                              The Tyler Perry melodrama follows a woman who undergoes trials brought on by a cheating husband (Lyriq Bent).
## 2                                              A story of obsession plays out in “Wilde Salomé” and “Salomé,” as Mr. Pacino veers into Camp.
## 3                                              A story of obsession plays out in “Wilde Salomé” and “Salomé,” as Mr. Pacino veers into Camp.
## 4 The brainchild of the writer Ishmael Reed and the director Bill Gunn, this film is an uncategorizable work of art — and an engrossing one.
## 5            Russ Harbaugh’s debut feature delivers something rarely seen in American movies: a warts-and-all examination of extended grief.
## 6     Edie Falco and Jay Duplass connect romantically in this life-after-prison drama that prioritizes patience and pragmatism over passion.
##   results.publication_date results.opening_date results.multimedia.type
## 1               2018-03-30           2018-03-30     mediumThreeByTwo210
## 2               2018-03-29           2017-04-01     mediumThreeByTwo210
## 3               2018-03-29                 <NA>     mediumThreeByTwo210
## 4               2018-03-29                 <NA>     mediumThreeByTwo210
## 5               2018-03-29           2018-03-30     mediumThreeByTwo210
## 6               2018-03-29           2018-04-03     mediumThreeByTwo210
#Renamed the columns 
colnames(movies_updated) <- c("Number_Results", "Title", "Mpaa_Rating", "Critics_Pick", "Byline", "Headline","Short_Summary","Publication_Date","Opening_Date","MM_Type")
head(movies_updated)
##   Number_Results                  Title Mpaa_Rating Critics_Pick
## 1             20 Tyler Perry's Acrimony           R            0
## 2             20           Wilde Salomé           R            1
## 3             20                 Salomé           R            1
## 4             20      Personal Problems                        1
## 5             20        Love After Love                        1
## 6             20             Outside In                        1
##                Byline
## 1      BEN KENIGSBERG
## 2         GLENN KENNY
## 3         GLENN KENNY
## 4         GLENN KENNY
## 5 JEANNETTE CATSOULIS
## 6 JEANNETTE CATSOULIS
##                                                                        Headline
## 1      Review: In ‘Acrimony,’ Taraji P. Henson Endures Marriage and Script Woes
## 2                             Review: Al Pacino’s Journey With Wilde’s ‘Salomé’
## 3                             Review: Al Pacino’s Journey With Wilde’s ‘Salomé’
## 4 Review: ‘Personal Problems,’ a Look at African-American Life in 1980 New York
## 5           Review: In ‘Love After Love,’ an Unflinching Look at Extended Grief
## 6                                 Review: On Parole and in Love in ‘Outside In’
##                                                                                                                                Short_Summary
## 1                              The Tyler Perry melodrama follows a woman who undergoes trials brought on by a cheating husband (Lyriq Bent).
## 2                                              A story of obsession plays out in “Wilde Salomé” and “Salomé,” as Mr. Pacino veers into Camp.
## 3                                              A story of obsession plays out in “Wilde Salomé” and “Salomé,” as Mr. Pacino veers into Camp.
## 4 The brainchild of the writer Ishmael Reed and the director Bill Gunn, this film is an uncategorizable work of art — and an engrossing one.
## 5            Russ Harbaugh’s debut feature delivers something rarely seen in American movies: a warts-and-all examination of extended grief.
## 6     Edie Falco and Jay Duplass connect romantically in this life-after-prison drama that prioritizes patience and pragmatism over passion.
##   Publication_Date Opening_Date             MM_Type
## 1       2018-03-30   2018-03-30 mediumThreeByTwo210
## 2       2018-03-29   2017-04-01 mediumThreeByTwo210
## 3       2018-03-29         <NA> mediumThreeByTwo210
## 4       2018-03-29         <NA> mediumThreeByTwo210
## 5       2018-03-29   2018-03-30 mediumThreeByTwo210
## 6       2018-03-29   2018-04-03 mediumThreeByTwo210
kable(movies_updated)
Number_Results Title Mpaa_Rating Critics_Pick Byline Headline Short_Summary Publication_Date Opening_Date MM_Type
20 Tyler Perry’s Acrimony R 0 BEN KENIGSBERG Review: In ‘Acrimony,’ Taraji P. Henson Endures Marriage and Script Woes The Tyler Perry melodrama follows a woman who undergoes trials brought on by a cheating husband (Lyriq Bent). 2018-03-30 2018-03-30 mediumThreeByTwo210
20 Wilde Salomé R 1 GLENN KENNY Review: Al Pacino’s Journey With Wilde’s ‘Salomé’ A story of obsession plays out in “Wilde Salomé” and “Salomé,” as Mr. Pacino veers into Camp. 2018-03-29 2017-04-01 mediumThreeByTwo210
20 Salomé R 1 GLENN KENNY Review: Al Pacino’s Journey With Wilde’s ‘Salomé’ A story of obsession plays out in “Wilde Salomé” and “Salomé,” as Mr. Pacino veers into Camp. 2018-03-29 NA mediumThreeByTwo210
20 Personal Problems 1 GLENN KENNY Review: ‘Personal Problems,’ a Look at African-American Life in 1980 New York The brainchild of the writer Ishmael Reed and the director Bill Gunn, this film is an uncategorizable work of art — and an engrossing one. 2018-03-29 NA mediumThreeByTwo210
20 Love After Love 1 JEANNETTE CATSOULIS Review: In ‘Love After Love,’ an Unflinching Look at Extended Grief Russ Harbaugh’s debut feature delivers something rarely seen in American movies: a warts-and-all examination of extended grief. 2018-03-29 2018-03-30 mediumThreeByTwo210
20 Outside In 1 JEANNETTE CATSOULIS Review: On Parole and in Love in ‘Outside In’ Edie Falco and Jay Duplass connect romantically in this life-after-prison drama that prioritizes patience and pragmatism over passion. 2018-03-29 2018-04-03 mediumThreeByTwo210
20 Gemini R 0 MANOHLA DARGIS Review: ‘Gemini’ Finds Murder in a Movie-Soaked Los Angeles The director Aaron Katz plays with film noir conventions in this pleasurably drifty, low-wattage mystery. 2018-03-29 2018-03-30 mediumThreeByTwo210
20 The Gardener 0 HELEN T. VERONGOS Review: ‘The Gardener’ Cultivates the Wealth of Nature Frank Cabot’s garden in Quebec is in part an amalgamation of others that inspired him in his travels. 2018-03-29 2018-03-30 mediumThreeByTwo210
20 After Louie 0 KEN JAWOROWSKI Review: ‘After Louie’ Looks at an Activist Once the Fighting Ends The film, starring Alan Cumming, centers on a 50-something man who lived through the worst years of the AIDS epidemic. 2018-03-29 2018-03-30 mediumThreeByTwo210
20 All I Wish 0 TEO BUGBEE Review: Sharon Stone Stars in ‘All I Wish,’ a Lukewarm Romantic Comedy A fine performance by Ms. Stone is up against a generic screenplay. 2018-03-29 2018-03-30 mediumThreeByTwo210
20 The Last Movie Star R 0 BEN KENIGSBERG Review: Burt Reynolds Confronts His Past in ‘The Last Movie Star’ The film effectively allows the ever-assured actor to score a touchdown on an empty field. 2018-03-29 2018-03-30 mediumThreeByTwo210
20 The China Hustle R 0 BEN KENIGSBERG Review: ‘The China Hustle’ Warns of Dicey Investments A documentary may be the wrong delivery mechanism for the byzantine exposé that cries out for detailed news reporting. 2018-03-29 2018-03-23 mediumThreeByTwo210
20 Finding Your Feet PG-13 0 JEANNETTE CATSOULIS Review: Two-Stepping to Happiness in ‘Finding Your Feet’ Led by a charming pairing of Imelda Staunton and Celia Imrie, this British retiree rom-com delivers exactly what it promises. 2018-03-29 NA mediumThreeByTwo210
20 Ready Player One PG-13 0 A. O. SCOTT Review: In ‘Ready Player One,’ Spielberg Plays the Nostalgia Game The movie, based on Ernest Cline’s best-selling novel, is set in a dystopian future where a virtual video-game reality reigns, and pop-culture callbacks are legion. 2018-03-28 2018-03-29 mediumThreeByTwo210
20 The Great Silence Not Rated 0 A. O. SCOTT Review: ‘The Great Silence,’ a 1968 Spaghetti Western Unchained Klaus Kinski and Jean-Louis Trintignant face off in the snows of Utah in a bloody genre exercise that is only now receiving a proper U.S. release. 2018-03-28 1969-00-00 mediumThreeByTwo210
20 Midnight Sun PG-13 0 TEO BUGBEE Review: In ‘Midnight Sun,’ the Dawn of Romance Can Be Fatal Bella Thorne stars as a teenager with a rare disease who must avoid ultraviolet light — but she isn’t avoiding love. One night, she meets a boy. 2018-03-23 2018-03-23 mediumThreeByTwo210
20 Ismael’s Ghosts R 1 GLENN KENNY Review: Wife Returns in ‘Ismael’s Ghosts.’ Confusion Ensues. In Arnaud Desplechin’s remarkable sprawl of a film, a wife comes back after a 20-year disappearance, adding to an already convoluted situation. 2018-03-22 2018-03-23 mediumThreeByTwo210
20 Beauty and the Dogs 1 JEANNETTE CATSOULIS Review: Seeking Justice After a Rape in ‘Beauty and the Dogs’ In this film inspired by a true story, the director Kaouther Ben Hania leans hard on official corruption and a bureaucracy dominated by male power. 2018-03-22 NA mediumThreeByTwo210
20 The Workshop 1 JENNIFER SZALAI Review: ‘The Workshop’ Plunges Into a Class Divided This new film from the French director Laurent Cantet follows a diverse group of students enrolled in a summer writing class taught by a famous novelist. 2018-03-22 NA mediumThreeByTwo210
20 I Kill Giants Not Rated 1 JEANNETTE CATSOULIS Review: Slaying Monsters (and Avoiding Grief) in ‘I Kill Giants’ This magical movie marries adult themes and childlike wonder as a troubled girl embarks on a fantastical quest. 2018-03-22 2018-03-23 mediumThreeByTwo210