Assignment Prompt

The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis 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 into an R DataFrame.

Retreive Data from API

I sign up for an API key on the New York Times website. I will be retrieving JSON data related to movie reviews

Connect to New York Times API

We will use GET() function from the HTTR package to make a request to retrieve data from New York Times API. The status is 200, which indicates a successful request. A status of 401 indicates a unsuccessful request due to unauthorization. For more information regarding other status number, look here .

library(jsonlite)
library(httr)
library(DT)

movies_url <- "https://api.nytimes.com/svc/movies/v2/reviews/picks.json?&api-key=lUEiZ4B7FNb2Nh9v3Xpy8w14vTaPAapg"
mov_response  = GET(movies_url) #response from the request GET()

mov_response
## Response [https://api.nytimes.com/svc/movies/v2/reviews/picks.json?&api-key=lUEiZ4B7FNb2Nh9v3Xpy8w14vTaPAapg]
##   Date: 2023-03-27 02:19
##   Status: 200
##   Content-Type: application/json; charset=utf-8
##   Size: 15.7 kB
article_url <- "https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key=W3DrQnpNWXQ253JBiRWf6Vo0AX1w1BSI"

article_response = GET(article_url)

article_response
## Response [https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key=W3DrQnpNWXQ253JBiRWf6Vo0AX1w1BSI]
##   Date: 2023-03-27 02:19
##   Status: 200
##   Content-Type: application/json; charset=utf-8
##   Size: 34.2 kB

JSON Data

Movie Reviews

NYT_content <- rawToChar(mov_response$content)

data = fromJSON(rawToChar(mov_response$content), flatten = TRUE)
names(data)
## [1] "status"      "copyright"   "has_more"    "num_results" "results"
data <- as.data.frame(data)

data
##    status                                                           copyright
## 1      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 2      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 3      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 4      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 5      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 6      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 7      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 8      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 9      OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 10     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 11     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 12     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 13     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 14     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 15     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 16     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 17     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 18     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 19     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
## 20     OK Copyright (c) 2023 The New York Times Company. All Rights Reserved.
##    has_more num_results                results.display_title
## 1      TRUE          20                      Tori and Lokita
## 2      TRUE          20                       The Worst Ones
## 3      TRUE          20                       Petite Solange
## 4      TRUE          20                               Reggie
## 5      TRUE          20 Nam June Paik: Moon Is the Oldest TV
## 6      TRUE          20                              Walk Up
## 7      TRUE          20                        The Lost King
## 8      TRUE          20                      The Five Devils
## 9      TRUE          20                               Rimini
## 10     TRUE          20                            Drylongso
## 11     TRUE          20                         The Innocent
## 12     TRUE          20                       Chang Can Dunk
## 13     TRUE          20                         Stonewalling
## 14     TRUE          20                                Punch
## 15     TRUE          20                         Therapy Dogs
## 16     TRUE          20                    Split at the Root
## 17     TRUE          20                             Linoleum
## 18     TRUE          20            A House Made of Splinters
## 19     TRUE          20                                Emily
## 20     TRUE          20              Huesera: The Bone Woman
##    results.mpaa_rating results.critics_pick      results.byline
## 1                                         1      Manohla Dargis
## 2                                         1        Lisa Kennedy
## 3                                         1   Natalia Winkelman
## 4                PG-13                    1         Glenn Kenny
## 5                                         1      Nicolas Rapold
## 6                                         1    Austin Considine
## 7                PG-13                    1 Jeannette Catsoulis
## 8                                         1     Beatrice Loayza
## 9                                         1     Beatrice Loayza
## 10                   R                    1        Lisa Kennedy
## 11                                        1      Claire Shaffer
## 12                  PG                    1         Calum Marsh
## 13                                        1      Manohla Dargis
## 14                                        1         Kyle Turner
## 15                                        1          Brandon Yu
## 16                                        1  Concepción de León
## 17                                        1 Jeannette Catsoulis
## 18                                        1      Nicolas Rapold
## 19                   R                    1      Manohla Dargis
## 20                                        1   Natalia Winkelman
##                                                                 results.headline
## 1                            ‘Tori and Lokita’ Review: Precarious Lives in Exile
## 2                                 ‘The Worst Ones’ Review: The Gazes of Children
## 3                 ‘Petite Solange’ Review: Coming of Age as Your Parents Divorce
## 4          ‘Reggie’ Review: Reggie Jackson on Himself, Racism and, Yes, Baseball
## 5                    ‘Nam June Paik: Moon Is the Oldest TV’ Review: Art Onscreen
## 6                              ‘Walk Up’ Review: Good Friends Make Bad Neighbors
## 7                                      ‘The Lost King’ Review: A Royal Obsession
## 8                                ‘The Five Devils’ Review: The Scent of the Past
## 9                                       ‘Rimini’ Review: Just an Austrian Gigolo
## 10                                             ‘Drylongso’ Review: Extraordinary
## 11                           ‘The Innocent’ Review: A Heist With a French Accent
## 12                                   ‘Chang Can Dunk’ Review: That’s a Man’s Jam
## 13                         ‘Stonewalling’ Review: A Young Woman’s Exchange Value
## 14                                          ‘Punch’ Review: Hitting at the Heart
## 15                                   ‘Therapy Dogs’ Review: It’s Time to Be Real
## 16 ‘Split at the Root’ Review: A Powerful Lens on Immigrant Families Split Apart
## 17                                                ‘Linoleum’ Review: Rocket Plan
## 18                 ‘A House Made of Splinters’ Review: Home Is Where the Hope Is
## 19                 ‘Emily’ Review: A Brontë Sister’s Savage, Hardy and Free Life
## 20         ‘Huesera: The Bone Woman’ Review: What to Dread When You’re Expecting
##                                                                                                                                     results.summary_short
## 1                                   In the new movie from the Dardenne brothers, two underage African migrants struggle to make a home in an unkind land.
## 2                                      In their feature, the directors Lisa Akoka and Romane Gueret build a provocative critique of filmmaking practices.
## 3       Axelle Ropert’s carefully calibrated film from France follows a girl experiencing the pain of having to accept her parents as people with faults.
## 4        Jackson, a.k.a. Mr. October, was called a lot of things during his storied career with the Yankees. A new documentary goes beyond the nicknames.
## 5                   A new documentary follows the ceaseless innovations of a man who made art out of television sets and found inspiration in disruption.
## 6            Hong Sang-soo’s latest film traces the relationships in a small Seoul apartment building as they evolve and grow heavier with complications.
## 7                                       Sally Hawkins lights a fire under this droll dramedy about the search for the final resting place of Richard III.
## 8      Part queer love story, part supernatural psychodrama, the uncanny second feature by Léa Mysius follows a young girl with a magical sense of smell.
## 9                  The director Ulrich Seidl’s unsettling drama tracks the exploitative behaviors of an aging lounge singer, his fans and family members.
## 10                                                Cauleen Smith’s 1998 movie, set in Oakland in the mid-90s, remains a vivid and prescient feature debut.
## 11                                                                A light, enjoyable confection of a film that is built upon an amusingly absurd premise.
## 12                                               A short teen channels his inner Kobe Bryant in this exhilarating, warmhearted Disney+ basketball comedy.
## 13 In this haunting drama, a 20-year-old Chinese woman struggles to find her place in a country where she faces a series of seemingly impossible hurdles.
## 14                                                   Welby Ings’s keenly observant debut feature follows a young, promising boxer whose priorities shift.
## 15                          This low-budget film by a pair of high schoolers offers a bracing, impressionistic portrait of senior year as it’s happening.
## 16                   This documentary shows the plight of one woman as she tries to reunite with her sons and make a permanent home in the United States.
## 17                                                Jim Gaffigan plays a dual role as rival scientists in this mysterious yet surprisingly effective movie.
## 18                   This film, an Oscar nominee this year for best documentary feature, has an aching sensitivity for the children in a Ukraine shelter.
## 19           Blending fact with generous, liberating fiction, the director Frances O’Connor brings the author of “Wuthering Heights” to pleasurable life.
## 20  In Michelle Garza Cervera’s terrifying and transfixing debut feature, a pregnant woman battles visions of a demoness who threatens her body and mind.
##    results.publication_date results.opening_date results.date_updated
## 1                2023-03-23                 <NA>  2023-03-23 17:46:51
## 2                2023-03-23           2023-03-24  2023-03-23 16:44:03
## 3                2023-03-23                 <NA>  2023-03-23 15:29:03
## 4                2023-03-23           2023-03-24  2023-03-23 11:05:02
## 5                2023-03-23           2023-03-24  2023-03-23 11:07:02
## 6                2023-03-23                 <NA>  2023-03-23 11:06:03
## 7                2023-03-23                 <NA>  2023-03-23 11:02:10
## 8                2023-03-23                 <NA>  2023-03-23 11:01:03
## 9                2023-03-16                 <NA>  2023-03-16 18:56:03
## 10               2023-03-16                 <NA>  2023-03-16 14:24:50
## 11               2023-03-16                 <NA>  2023-03-16 11:04:02
## 12               2023-03-10           2023-03-10  2023-03-10 08:02:03
## 13               2023-03-09                 <NA>  2023-03-09 19:08:05
## 14               2023-03-09                 <NA>  2023-03-09 16:17:03
## 15               2023-03-08                 <NA>  2023-03-08 12:01:03
## 16               2023-03-02                 <NA>  2023-03-02 12:03:10
## 17               2023-02-23                 <NA>  2023-02-23 12:02:04
## 18               2023-02-22           2023-02-21  2023-02-22 19:47:03
## 19               2023-02-16                 <NA>  2023-02-16 18:18:03
## 20               2023-02-09           2023-02-10  2023-02-09 15:33:15
##    results.link.type
## 1            article
## 2            article
## 3            article
## 4            article
## 5            article
## 6            article
## 7            article
## 8            article
## 9            article
## 10           article
## 11           article
## 12           article
## 13           article
## 14           article
## 15           article
## 16           article
## 17           article
## 18           article
## 19           article
## 20           article
##                                                                                   results.link.url
## 1                   https://www.nytimes.com/2023/03/23/movies/tori-and-lokita-review-dardenne.html
## 2                             https://www.nytimes.com/2023/03/23/movies/the-worst-ones-review.html
## 3                             https://www.nytimes.com/2023/03/23/movies/petite-solange-review.html
## 4                                     https://www.nytimes.com/2023/03/23/movies/reggie-review.html
## 5        https://www.nytimes.com/2023/03/23/movies/nam-june-paik-moon-is-the-oldest-tv-review.html
## 6                                    https://www.nytimes.com/2023/03/23/movies/walk-up-review.html
## 7                              https://www.nytimes.com/2023/03/23/movies/the-lost-king-review.html
## 8                            https://www.nytimes.com/2023/03/23/movies/the-five-devils-review.html
## 9                                     https://www.nytimes.com/2023/03/16/movies/rimini-review.html
## 10                                 https://www.nytimes.com/2023/03/16/movies/drylongso-review.html
## 11 https://www.nytimes.com/2023/03/16/movies/the-innocent-review-a-heist-with-a-french-accent.html
## 12                            https://www.nytimes.com/2023/03/10/movies/chang-can-dunk-review.html
## 13                              https://www.nytimes.com/2023/03/09/movies/stonewalling-review.html
## 14                                     https://www.nytimes.com/2023/03/09/movies/punch-review.html
## 15                              https://www.nytimes.com/2023/03/08/movies/therapy-dogs-review.html
## 16                         https://www.nytimes.com/2023/03/02/movies/split-at-the-root-review.html
## 17                      https://www.nytimes.com/2023/02/23/movies/linoleum-review-rocket-plan.html
## 18                 https://www.nytimes.com/2023/02/22/movies/a-house-made-of-splinters-review.html
## 19            https://www.nytimes.com/2023/02/16/movies/emily-review-bronte-wuthering-heights.html
## 20                    https://www.nytimes.com/2023/02/09/movies/huesera-the-bone-woman-review.html
##                                          results.link.suggested_link_text
## 1                       Read the New York Times Review of Tori and Lokita
## 2                        Read the New York Times Review of The Worst Ones
## 3                        Read the New York Times Review of Petite Solange
## 4                                Read the New York Times Review of Reggie
## 5  Read the New York Times Review of Nam June Paik: Moon Is the Oldest TV
## 6                               Read the New York Times Review of Walk Up
## 7                         Read the New York Times Review of The Lost King
## 8                       Read the New York Times Review of The Five Devils
## 9                                Read the New York Times Review of Rimini
## 10                            Read the New York Times Review of Drylongso
## 11                         Read the New York Times Review of The Innocent
## 12                       Read the New York Times Review of Chang Can Dunk
## 13                         Read the New York Times Review of Stonewalling
## 14                                Read the New York Times Review of Punch
## 15                         Read the New York Times Review of Therapy Dogs
## 16                    Read the New York Times Review of Split at the Root
## 17                             Read the New York Times Review of Linoleum
## 18            Read the New York Times Review of A House Made of Splinters
## 19                                Read the New York Times Review of Emily
## 20              Read the New York Times Review of Huesera: The Bone Woman
##    results.multimedia.type
## 1      mediumThreeByTwo210
## 2      mediumThreeByTwo210
## 3      mediumThreeByTwo210
## 4      mediumThreeByTwo210
## 5      mediumThreeByTwo210
## 6      mediumThreeByTwo210
## 7      mediumThreeByTwo210
## 8      mediumThreeByTwo210
## 9      mediumThreeByTwo210
## 10     mediumThreeByTwo210
## 11     mediumThreeByTwo210
## 12     mediumThreeByTwo210
## 13     mediumThreeByTwo210
## 14     mediumThreeByTwo210
## 15     mediumThreeByTwo210
## 16     mediumThreeByTwo210
## 17     mediumThreeByTwo210
## 18     mediumThreeByTwo210
## 19     mediumThreeByTwo210
## 20     mediumThreeByTwo210
##                                                                                                                                    results.multimedia.src
## 1                                         https://static01.nyt.com/images/2023/03/23/multimedia/tori-lokita-mplb/tori-lokita-mplb-mediumThreeByTwo440.jpg
## 2                      https://static01.nyt.com/images/2023/03/23/multimedia/23worst-ones-review-jhpw/23worst-ones-review-jhpw-mediumThreeByTwo440-v4.jpg
## 3                                                 https://static01.nyt.com/images/2023/03/22/multimedia/petite1-cftq/petite1-cftq-mediumThreeByTwo440.jpg
## 4                                 https://static01.nyt.com/images/2023/03/23/multimedia/23reggie-review-jcqg/23reggie-review-jcqg-mediumThreeByTwo440.jpg
## 5                 https://static01.nyt.com/images/2023/03/24/multimedia/23nam-june-paik-review1-kthm/23nam-june-paik-review1-kthm-mediumThreeByTwo440.jpg
## 6                                                 https://static01.nyt.com/images/2023/03/24/multimedia/walkup1-kzml/walkup1-kzml-mediumThreeByTwo440.jpg
## 7                                             https://static01.nyt.com/images/2023/03/22/multimedia/lostking1-zklj/lostking1-zklj-mediumThreeByTwo440.jpg
## 8                                         https://static01.nyt.com/images/2023/03/22/multimedia/fivedevils1-wjpl/fivedevils1-wjpl-mediumThreeByTwo440.jpg
## 9                                 https://static01.nyt.com/images/2023/03/16/multimedia/16rimini-review-chpq/16rimini-review-chpq-mediumThreeByTwo440.jpg
## 10                          https://static01.nyt.com/images/2023/03/17/multimedia/16drylongso-review-fvcl/16drylongso-review-fvcl-mediumThreeByTwo440.jpg
## 11                  https://static01.nyt.com/images/2023/03/16/multimedia/16innocent-review-pix1-kbfj/16innocent-review-pix1-kbfj-mediumThreeByTwo440.jpg
## 12                                                  https://static01.nyt.com/images/2023/03/10/multimedia/chang1-vqgp/chang1-vqgp-mediumThreeByTwo440.jpg
## 13                                https://static01.nyt.com/images/2023/03/09/multimedia/09stonewalling2-lmgz/09stonewalling2-lmgz-mediumThreeByTwo440.jpg
## 14                                  https://static01.nyt.com/images/2023/03/09/multimedia/09punch-review-vkzq/09punch-review-vkzq-mediumThreeByTwo440.jpg
## 15               https://static01.nyt.com/images/2023/03/08/multimedia/08therapy-dogs-review1-pvct/08therapy-dogs-review1-pvct-mediumThreeByTwo440-v2.jpg
## 16          https://static01.nyt.com/images/2023/03/03/multimedia/02split-at-the-root-review-wcgq/02split-at-the-root-review-wcgq-mediumThreeByTwo440.jpg
## 17                                              https://static01.nyt.com/images/2023/02/24/multimedia/linoleum-kvgh/linoleum-kvgh-mediumThreeByTwo440.jpg
## 18 https://static01.nyt.com/images/2023/02/24/multimedia/22house-made-of-splinters-art-bptg/22house-made-of-splinters-art-bptg-mediumThreeByTwo440-v2.jpg
## 19                                  https://static01.nyt.com/images/2023/02/16/multimedia/16emily-review-bmqz/16emily-review-bmqz-mediumThreeByTwo440.jpg
## 20                                              https://static01.nyt.com/images/2023/02/08/multimedia/huesera1-zmtl/huesera1-zmtl-mediumThreeByTwo440.jpg
##    results.multimedia.height results.multimedia.width
## 1                        140                      210
## 2                        140                      210
## 3                        140                      210
## 4                        140                      210
## 5                        140                      210
## 6                        140                      210
## 7                        140                      210
## 8                        140                      210
## 9                        140                      210
## 10                       140                      210
## 11                       140                      210
## 12                       140                      210
## 13                       140                      210
## 14                       140                      210
## 15                       140                      210
## 16                       140                      210
## 17                       140                      210
## 18                       140                      210
## 19                       140                      210
## 20                       140                      210

JSON data to Dataframe

Movies Review: Critic’s Pick

The data contains recent 20 critic’s movie picks on New York Times.

Remove columns

We do not need the following columns: status”, “has_more”, “num_results”, “results.opening_date”, “results.date_updated”, “results.link.type”, “results.link.suggested_link_text”, “results.multimedia.height”, “results.multimedia.width”.

Since most of the values in the ‘mpaa_rating’ column are missing, this column will be dropped.

names(data)
##  [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.height"        "results.multimedia.width"
tidied_data <- data[c("copyright", "results.display_title", "results.critics_pick", "results.byline","results.headline", "results.summary_short","results.publication_date","results.link.url","results.multimedia.src" )]                      
colnames(tidied_data) <- c("copyright", "film_title", "critics_pick", "critic_name","NYT_headline", "short_summary","publication_date","link.url","multimedia.src" )

tidied_data <- tidied_data[c( "film_title","multimedia.src", "critics_pick", "critic_name","NYT_headline", "short_summary","publication_date","link.url", "copyright" )]

datatable(tidied_data,options = list(scrollX = TRUE), colnames=c("Movie Title","Movie Image", "Critic's Pick", "Critic Name","NYT Headline", "Short Summary","Publication Date","Article URL", "Copyright"))

Display images

There is column that provides the images associated with the movie. In order to display, we need to append “<img src=” in front of every url and “‘height=’200’>” at the end of each URL.

# The code below helps us show the images from the provided URL in the 'multimedia.src' column. This code requires manual work of inserting each unique link
#tidied_data$multimedia.src[grep("https://static01.nyt.com/images/2023/03/23/multimedia/tori-lokita-mplb/tori-lokita-mplb-mediumThreeByTwo440.jpg", tidied_data$multimedia.src)] <- "<img src='https://static01.nyt.com/images/2023/03/23/multimedia/tori-lokita-mplb/tori-lokita-mplb-mediumThreeByTwo440.jpg'height='200'></img>' 

# The code below helps us show the images from the provided URL in the 'multimedia.src' column. It is easier than above. 

tidied_data$multimedia.src <- paste("<img src='", tidied_data$multimedia.src)
tidied_data$multimedia.src <- paste(tidied_data$multimedia.src, "'height='150'width='300'></img>")

datatable(tidied_data,options = list(scrollX = TRUE), escape = FALSE, colnames=c("Movie Title","multimedia.src", "Critic's Pick", "Critic Name","NYT Headline", "Short Summary","Publication Date","Article URL", "Copyright"))

Source

  1. The link below provide information on how to request data from API in R. https://www.dataquest.io/blog/r-api-tutorial/

  2. The link below provides the formatting of inserting images. https://stackoverflow.com/questions/62970417/rendering-images-in-dt-table-in-r-shiny