Introduction

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.

Loading of the required libraries

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

API key

The generated APIkey is stored in a varibale for accessing the API

apiKey <- "qkNkWmUI086c7ZaZVk3ptPQN0FUBwR7K"

Most viewed news

I have chosen the API which gives most viewed news for past 7 days on NYT

url <- paste0("https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json?api-key=",apiKey)
mostviewed <- GET(url)

Check for the status code of the response.

#status_code == 200 Response complete
mostviewed$status_code
## [1] 200

Read the response into a dataframe

mostviewed <- fromJSON(url, flatten = TRUE)
df <- mostviewed$results
head(df)
##                                                  uri
## 1 nyt://article/c1b25e90-e5d8-536a-b46e-ac92b957b249
## 2 nyt://article/80ebc2b0-44c7-584f-bb05-0fa01ba93586
## 3 nyt://article/056561cc-ff8f-5ba7-a363-797857695dd0
## 4 nyt://article/05277442-70b4-55c6-a6f1-ce797963d9b7
## 5 nyt://article/a053cc3d-7389-5ce3-ab30-f7f99ec600b0
## 6 nyt://article/0f0d3fd9-34ff-517f-b8fa-d3e7d5c72ed8
##                                                                                          url
## 1                               https://www.nytimes.com/2020/10/20/us/trump-taxes-china.html
## 2 https://www.nytimes.com/2020/10/19/business/media/jeffrey-toobin-new-yorker-suspended.html
## 3    https://www.nytimes.com/2020/10/21/world/europe/pope-francis-same-sex-civil-unions.html
## 4          https://www.nytimes.com/2020/10/18/business/media/new-york-post-hunter-biden.html
## 5     https://www.nytimes.com/2020/10/17/style/sacha-baron-cohen-maureen-dowd-interview.html
## 6              https://www.nytimes.com/2020/10/16/us/politics/republican-senators-trump.html
##      id asset_id         source published_date             updated  section
## 1 1e+14    1e+14 New York Times     2020-10-20 2020-10-23 02:53:22     U.S.
## 2 1e+14    1e+14 New York Times     2020-10-19 2020-10-20 14:14:48 Business
## 3 1e+14    1e+14 New York Times     2020-10-21 2020-10-23 09:19:15    World
## 4 1e+14    1e+14 New York Times     2020-10-18 2020-10-22 20:25:03 Business
## 5 1e+14    1e+14 New York Times     2020-10-17 2020-10-23 13:40:55    Style
## 6 1e+14    1e+14 New York Times     2020-10-16 2020-10-18 13:19:19     U.S.
##   subsection nytdsection
## 1                   u.s.
## 2      Media    business
## 3     Europe       world
## 4      Media    business
## 5                  style
## 6   Politics        u.s.
##                                                                                                                                                                                                                                                                          adx_keywords
## 1 Presidential Election of 2020;Trump Tax Returns;Conflicts of Interest;United States Politics and Government;Trump, Donald J;Biden, Joseph R Jr;Biden, Hunter;Ruffin, Phil;Federal Bureau of Investigation;Senate;Trump Organization;Trump Tower (Manhattan, NY);United States;China
## 2                                                                                                                                                                                          Videophones and Videoconferencing;Toobin, Jeffrey;New Yorker;CNN;Zoom Video Communications
## 3                                                                                                    Same-Sex Marriage, Civil Unions and Domestic Partnerships;Documentary Films and Programs;Homosexuality and Bisexuality;Francis;Afineevsky, Evgeny (1972- );Roman Catholic Church
## 4                                                       Presidential Election of 2020;News and News Media;Conflicts of Interest;Bannon, Stephen K;Biden, Hunter;Biden, Joseph R Jr;Giuliani, Rudolph W;Murdoch, Rupert;Murdoch, Lachlan;Allan, Col;Burisma Holdings Ltd;New York Post
## 5                                                                                                                                                                                      Actors and Actresses;Comedy and Humor;Movies;Content Type: Personal Profile;Cohen, Sacha Baron
## 6                                                                                                                                                       Elections, Senate;Presidential Election of 2020;United States Politics and Government;Trump, Donald J;Republican Party;Senate
##   column                                            byline    type
## 1     NA By Mike McIntire, Russ Buettner and Susanne Craig Article
## 2     NA                   By Johnny Diaz and Azi Paybarah Article
## 3     NA                                 By Jason Horowitz Article
## 4     NA                                By Katie Robertson Article
## 5     NA                                   By Maureen Dowd Article
## 6     NA                                By Catie Edmondson Article
##                                                                        title
## 1                  Trump Records Shed New Light on Chinese Business Pursuits
## 2                     New Yorker Suspends Jeffrey Toobin After Zoom Incident
## 3 In Shift for Church, Pope Francis Voices Support for Same-Sex Civil Unions
## 4           New York Post Published Hunter Biden Report Amid Newsroom Doubts
## 5                                  Sacha Baron Cohen: This Time He’s Serious
## 6  Fearing a ‘Blood Bath,’ Republican Senators Begin to Edge Away From Trump
##                                                                                                                                                                                                       abstract
## 1                         As he raises questions about his opponent’s standing with China, President Trump’s taxes reveal details about his own activities there, including a previously unknown bank account.
## 2                                                                                                  In a statement, the magazine said it was investigating a matter involving the author and CNN legal analyst.
## 3                                                                        The comments, shown in a new documentary, are the strongest yet from a pontificate that has taken a more tolerant and inclusive tone.
## 4                                                                          Some reporters withheld their bylines and questioned the credibility of an article that made the tabloid’s front page on Wednesday.
## 5                                 Reviving his Borat character and playing the political activist Abbie Hoffman, the actor feels he “had to ring the alarm bell and say that democracy is in peril this year.”
## 6 The statements offer an answer to the question of when Republicans might begin to repudiate the president after years of embracing him: the moment they believed he threatened their own political survival.
##                                                                                                                  des_facet
## 1           Presidential Election of 2020, Trump Tax Returns, Conflicts of Interest, United States Politics and Government
## 2                                                                                        Videophones and Videoconferencing
## 3 Same-Sex Marriage, Civil Unions and Domestic Partnerships, Documentary Films and Programs, Homosexuality and Bisexuality
## 4                                                Presidential Election of 2020, News and News Media, Conflicts of Interest
## 5                                           Actors and Actresses, Comedy and Humor, Movies, Content Type: Personal Profile
## 6                                  Elections, Senate, Presidential Election of 2020, United States Politics and Government
##                                                                                  org_facet
## 1 Federal Bureau of Investigation, Senate, Trump Organization, Trump Tower (Manhattan, NY)
## 2                                               New Yorker, CNN, Zoom Video Communications
## 3                                                                    Roman Catholic Church
## 4                                                      Burisma Holdings Ltd, New York Post
## 5                                                                                         
## 6                                                                 Republican Party, Senate
##                                                                                                                  per_facet
## 1                                                         Trump, Donald J, Biden, Joseph R Jr, Biden, Hunter, Ruffin, Phil
## 2                                                                                                          Toobin, Jeffrey
## 3                                                                                     Francis, Afineevsky, Evgeny (1972- )
## 4 Bannon, Stephen K, Biden, Hunter, Biden, Joseph R Jr, Giuliani, Rudolph W, Murdoch, Rupert, Murdoch, Lachlan, Allan, Col
## 5                                                                                                       Cohen, Sacha Baron
## 6                                                                                                          Trump, Donald J
##              geo_facet
## 1 United States, China
## 2                     
## 3                     
## 4                     
## 5                     
## 6                     
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      media
## 1                                                                                                           image, photo, President Trump at a 2017 meeting with the leader of China, Xi Jinping. Mr. Trump has a long history of chasing licensing deals in the country., Stephen Crowley/The New York Times, 1, https://static01.nyt.com/images/2020/10/20/multimedia/xxtrump-china-1/xxtrump-china-1-thumbStandard-v2.jpg, https://static01.nyt.com/images/2020/10/20/multimedia/xxtrump-china-1/xxtrump-china-1-mediumThreeByTwo210-v2.jpg, https://static01.nyt.com/images/2020/10/20/multimedia/xxtrump-china-1/xxtrump-china-1-mediumThreeByTwo440-v2.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 2                                                                                                                                               image, photo, “I made an embarrassingly stupid mistake, believing I was off-camera,” Jeffrey Toobin said in a statement., Vivien Killilea/Getty Images for PSIFF, 1, https://static01.nyt.com/images/2020/10/20/multimedia/20xp-toobin-2016/20xp-toobin-2016-thumbStandard.jpg, https://static01.nyt.com/images/2020/10/20/multimedia/20xp-toobin-2016/20xp-toobin-2016-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2020/10/20/multimedia/20xp-toobin-2016/20xp-toobin-2016-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 3                                                                                                                                                                         image, photo, Pope Francis arriving for the weekly general audience at the Vatican on Wednesday., Angelo Carconi/EPA, via Shutterstock, 1, https://static01.nyt.com/images/2020/10/21/world/21pope-civilunion/21pope-civilunion-thumbStandard-v2.jpg, https://static01.nyt.com/images/2020/10/21/world/21pope-civilunion/21pope-civilunion-mediumThreeByTwo210-v3.jpg, https://static01.nyt.com/images/2020/10/21/world/21pope-civilunion/21pope-civilunion-mediumThreeByTwo440-v3.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 4                                                                                                                                 image, photo, The offices of The New York Post. Staff members had concerns about the reliability of a front-page article’s sources and its late-campaign timing., Kevin Hagen/Getty Images, 1, https://static01.nyt.com/images/2020/10/18/lens/18nypost-building/18nypost-building-thumbStandard.jpg, https://static01.nyt.com/images/2020/10/18/lens/18nypost-building/18nypost-building-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2020/10/18/lens/18nypost-building/18nypost-building-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 5                                                                                                                            image, photo, Leading man: Sacha Baron Cohen’s riotous characters have perhaps masked his dramatic abilities., Buck Ellison for The New York Times, 1, https://static01.nyt.com/images/2020/10/18/fashion/17WITH-SACHABARONCOHEN1/17WITH-SACHABARONCOHEN1-thumbStandard.jpg, https://static01.nyt.com/images/2020/10/18/fashion/17WITH-SACHABARONCOHEN1/17WITH-SACHABARONCOHEN1-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2020/10/18/fashion/17WITH-SACHABARONCOHEN1/17WITH-SACHABARONCOHEN1-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 6 image, photo, Senators Ted Cruz, Lindsey Graham and John Cornyn on Wednesday at the Capitol. Mr. Cruz and Mr. Graham have both made remarks about possible Republican losses because of President Trump., Hilary Swift for The New York Times, 1, https://static01.nyt.com/images/2020/10/16/us/politics/16dc-repubs-1/16dc-repubs-1-thumbStandard.jpg, https://static01.nyt.com/images/2020/10/16/us/politics/16dc-repubs-1/merlin_178536195_0256de16-f901-406e-8767-cc439f6d69e7-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2020/10/16/us/politics/16dc-repubs-1/merlin_178536195_0256de16-f901-406e-8767-cc439f6d69e7-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
##   eta_id
## 1      0
## 2      0
## 3      0
## 4      0
## 5      0
## 6      0

Create a dataframe with required set of variables only

mostvieweddfdf <- data.frame(df$title,df$published_date,df$section,df$url)
head(mostvieweddfdf)
##                                                                     df.title
## 1                  Trump Records Shed New Light on Chinese Business Pursuits
## 2                     New Yorker Suspends Jeffrey Toobin After Zoom Incident
## 3 In Shift for Church, Pope Francis Voices Support for Same-Sex Civil Unions
## 4           New York Post Published Hunter Biden Report Amid Newsroom Doubts
## 5                                  Sacha Baron Cohen: This Time He’s Serious
## 6  Fearing a ‘Blood Bath,’ Republican Senators Begin to Edge Away From Trump
##   df.published_date df.section
## 1        2020-10-20       U.S.
## 2        2020-10-19   Business
## 3        2020-10-21      World
## 4        2020-10-18   Business
## 5        2020-10-17      Style
## 6        2020-10-16       U.S.
##                                                                                       df.url
## 1                               https://www.nytimes.com/2020/10/20/us/trump-taxes-china.html
## 2 https://www.nytimes.com/2020/10/19/business/media/jeffrey-toobin-new-yorker-suspended.html
## 3    https://www.nytimes.com/2020/10/21/world/europe/pope-francis-same-sex-civil-unions.html
## 4          https://www.nytimes.com/2020/10/18/business/media/new-york-post-hunter-biden.html
## 5     https://www.nytimes.com/2020/10/17/style/sacha-baron-cohen-maureen-dowd-interview.html
## 6              https://www.nytimes.com/2020/10/16/us/politics/republican-senators-trump.html

Let’s rename the variables to be descriptive

names(mostvieweddfdf)[1]<-"Title"
names(mostvieweddfdf)[2]<-"Date"
names(mostvieweddfdf)[3]<-"Section"
names(mostvieweddfdf)[4]<-"URL"

View the data over a data table

datatable(mostvieweddfdf)

Conculsion

We see from the result that news on Trump is the most viewed and as we are nearing to the election date this makes sense. Pope’s support for same sex marriage is also amongst the most viewed too.