Web APIs

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

Choice of New York Times API

I chose the Movie Reviews API, for which the documentation can be found here (requires API key): http://developer.nytimes.com/movie_reviews_v2.json#/README

Construct Interface, Read JSON Data, and Transform to R Dataframe

I installed the ‘jsonlite’ package to facilitate reading the JSON and transforming it to a data frame.

install.packages("jsonlite",repos='http://cran.wustl.edu/')
library(jsonlite)

The code below creates the R interface, reads in JSON data, and transforms it to a data frame. (Code borrows heavily from this CRAN R page: https://cran.r-project.org/web/packages/jsonlite/vignettes/json-apis.html)

# Use API key to create key string to be added to URL
nyt_api_key <- "&api-key=a4c5027d4f8e46e9bb938a232b600914"
# Assign API URL to variable; searches the movie reviews table using the key
# word "walk" for movies which opened between 1/1/1930 and 1/1/2015
nyt_api_url <- "http://api.nytimes.com/svc/movies/v2/reviews/search.json?query=walk&opening-date=1930-01-01;2015-01-01"
# Get JSON data from API and convert to data frame
movies <- data.frame(fromJSON(paste0(nyt_api_url, nyt_api_key)), simplifyDataFrame= TRUE)
class(movies)
## [1] "data.frame"
# View data frame
head(movies)
##   status
## 1     OK
## 2     OK
## 3     OK
## 4     OK
## 5     OK
## 6     OK
##                                                             copyright
## 1 Copyright (c) 2016 The New York Times Company. All Rights Reserved.
## 2 Copyright (c) 2016 The New York Times Company. All Rights Reserved.
## 3 Copyright (c) 2016 The New York Times Company. All Rights Reserved.
## 4 Copyright (c) 2016 The New York Times Company. All Rights Reserved.
## 5 Copyright (c) 2016 The New York Times Company. All Rights Reserved.
## 6 Copyright (c) 2016 The New York Times Company. All Rights Reserved.
##   has_more num_results                    results.display_title
## 1     TRUE          20              A Walk Among the Tombstones
## 2     TRUE          20                          The Maid's Room
## 3     TRUE          20 Walking the Camino: Six Ways to Santiago
## 4     TRUE          20                           Brick Mansions
## 5     TRUE          20                   Walking with the Enemy
## 6     TRUE          20                Walking With Dinosaurs 3D
##   results.mpaa_rating results.critics_pick  results.byline
## 1                   R                    0  MANOHLA DARGIS
## 2                                        0  BEN KENIGSBERG
## 3                                        0  DANIEL M. GOLD
## 4               PG-13                    0     A. O. SCOTT
## 5               PG-13                    0  BEN KENIGSBERG
## 6                  PG                    0 NEIL GENZLINGER
##                                  results.headline
## 1                     Good vs. Evil, Both Bloody 
## 2            The Invisible but All-Seeing Servant
## 3            A Journey for the Soles and the Soul
## 4       A Rocket Is Aimed at Detroit (Run! Jump!)
## 5 In a Scary Uniform, Subverting a March to Death
## 6 Like Simba, Only He<U+0092>s a Plucky Pachyrhinosaurus
##                                                                                                                                                                    results.summary_short
## 1    In &#8220;A Walk Among the Tombstones&#8221; the private investigator Matthew Scudder, from the Lawrence Block novels, pursues a case that leads him to an underworld of brutality.
## 2                                                                                           In <U+0093>The Maid<U+0092>s Room,<U+0094> a live-in maid in the Hamptons gains some leverage over her employers.
## 3                                                  <U+0093>Walking the Camino: Six Ways to Santiago<U+0094> documents travelers who make the trek to the Cathedral of Santiago de Compostela in Spain.
## 4 <U+0093>Brick Mansions,<U+0094> directed by Camille Delamarre, comes tantalizingly close to being a high-impact allegory of race, class and real estate in a postindustrial, new-Gilded Age America.
## 5                                           <U+0093>Walking With the Enemy<U+0094> is based on the real-life story of a Hungarian Jew who saved lives during World War II by posing as a Nazi officer.
## 6                                                       In the family-friendly film <U+0093>Walking With Dinosaurs,<U+0094> computer-generated creatures face conflicts echoing the <U+0093>Lion King<U+0094> story.
##   results.publication_date results.opening_date results.date_updated
## 1               2014-09-18           2014-09-19  2016-03-30 07:03:47
## 2               2014-08-07           2014-08-08  2016-03-30 07:01:11
## 3               2014-06-05           2013-02-27  2016-03-30 07:08:29
## 4               2014-04-24           2014-04-25  2016-03-30 07:02:02
## 5               2014-04-24           2014-04-15  2016-03-30 07:03:20
## 6               2013-12-19           2013-12-20  2016-03-30 07:00:21
##   results.link.type
## 1           article
## 2           article
## 3           article
## 4           article
## 5           article
## 6           article
##                                                                                         results.link.url
## 1 http://www.nytimes.com/2014/09/19/movies/a-walk-among-the-tombstones-adapts-lawrence-blocks-novel.html
## 2                http://www.nytimes.com/2014/08/08/movies/the-maids-room-directed-by-michael-walker.html
## 3        http://www.nytimes.com/2014/06/06/movies/walking-the-camino-a-documentary-by-lydia-b-smith.html
## 4    http://www.nytimes.com/2014/04/25/movies/paul-walker-stars-in-the-action-remake-brick-mansions.html
## 5          http://www.nytimes.com/2014/04/25/movies/a-jew-poses-as-a-nazi-in-walking-with-the-enemy.html
## 6          http://www.nytimes.com/2013/12/20/movies/walking-with-dinosaurs-about-a-herd-on-the-move.html
##                                             results.link.suggested_link_text
## 1              Read the New York Times Review of A Walk Among the Tombstones
## 2                          Read the New York Times Review of The Maid's Room
## 3 Read the New York Times Review of Walking the Camino: Six Ways to Santiago
## 4                           Read the New York Times Review of Brick Mansions
## 5                   Read the New York Times Review of Walking with the Enemy
## 6                Read the New York Times Review of Walking With Dinosaurs 3D
##   results.multimedia.type
## 1     mediumThreeByTwo210
## 2     mediumThreeByTwo210
## 3     mediumThreeByTwo210
## 4     mediumThreeByTwo210
## 5     mediumThreeByTwo210
## 6     mediumThreeByTwo210
##                                                                                               results.multimedia.src
## 1 https://static01.nyt.com/images/2014/09/19/arts/WALKAMONGTOMBSTONES/WALKAMONGTOMBSTONES-mediumThreeByTwo210-v3.jpg
## 2                            https://static01.nyt.com/images/2014/08/08/arts/08MAIDS/08MAIDS-mediumThreeByTwo210.jpg
## 3                          https://static01.nyt.com/images/2014/06/06/arts/06WALKING/WALKING-mediumThreeByTwo210.jpg
## 4                         https://static01.nyt.com/images/2014/04/25/arts/25BRICK/25BRICK-mediumThreeByTwo210-v2.jpg
## 5   https://static01.nyt.com/images/2014/04/25/arts/25RDP_WALKING_SPAN/25RDP_WALKING_SPAN-mediumThreeByTwo210-v2.jpg
## 6                      https://static01.nyt.com/images/2013/12/20/arts/20DINOSAURS/DINOSAURS-mediumThreeByTwo210.jpg
##   results.multimedia.width results.multimedia.height simplifyDataFrame
## 1                      210                       140              TRUE
## 2                      210                       140              TRUE
## 3                      210                       140              TRUE
## 4                      210                       140              TRUE
## 5                      210                       140              TRUE
## 6                      210                       140              TRUE