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.
The read me for the API is located here: http://developer.nytimes.com/most_popular_api_v2.json#/README. This API summarizes information on which articles were emailed, shared and/or viewed the most in a given time period.
The base URL for the API:
http://api.nytimes.com/svc/mostpopular/{version}/ {mostemailed | mostshared | mostviewed}
For the purposes of this assignment I am going to look at most shared articles for all sections in the last day.
The URL (including API-KEY) is: http://api.nytimes.com/svc/mostpopular/v2/mostshared/all-sections/1.json?api-key=363352eb8b794d0983a54f8352317680
library(jsonlite)
library(plyr)
rawJSON <- fromJSON("http://api.nytimes.com/svc/mostpopular/v2/mostshared/all-sections/1.json?api-key=363352eb8b794d0983a54f8352317680")
The plyr package has a function ldply that allows us to transform the list format generated by jsonlite directly into a data frame.
NYTMostPopular <- ldply(.data = rawJSON[4], data.frame) #the first 3 members of the list contain header information about the results
head(NYTMostPopular)
## .id
## 1 results
## 2 results
## 3 results
## 4 results
## 5 results
## 6 results
## url
## 1 http://www.nytimes.com/2016/10/29/us/politics/fbi-hillary-clinton-email.html
## 2 http://www.nytimes.com/2016/10/30/us/politics/hillary-clinton-emails-fbi-anthony-weiner.html
## 3 http://www.nytimes.com/2016/10/30/briefing/your-weekend-briefing.html
## 4 http://www.nytimes.com/2016/10/30/business/gmo-promise-falls-short.html
## 5 http://www.nytimes.com/2016/10/30/opinion/on-clinton-emails-did-the-fbi-director-abuse-his-power.html
## 6 http://www.nytimes.com/2016/10/30/us/politics/comey-clinton-email-justice.html
## column section
## 1 U.S.
## 2 U.S.
## 3 Briefing
## 4 Uncertain Harvest Business Day
## 5 Op-Ed Contributor Opinion
## 6 U.S.
## byline
## 1 By ADAM GOLDMAN and ALAN RAPPEPORT
## 2 By PATRICK HEALY and JONATHAN MARTIN
## 3 By KAREN WORKMAN and LISA IABONI
## 4 By DANNY HAKIM
## 5 By RICHARD W. PAINTER
## 6 By MATT APUZZO, ADAM GOLDMAN, MICHAEL S. SCHMIDT and WILLIAM K. RASHBAUM
## title
## 1 Emails in Anthony Weiner Inquiry Jolt Hillary Clinton’s Campaign
## 2 Hillary Clinton Assails James Comey, Calling Email Decision ‘Deeply Troubling’
## 3 Your Weekend Briefing
## 4 Doubts About the Promised Bounty of Genetically Modified Crops
## 5 On Clinton Emails, Did the F.B.I. Director Abuse His Power?
## 6 Justice Dept. Strongly Discouraged Comey on Move in Clinton Email Case
## abstract
## 1 The F.B.I. director, James B. Comey, told Congress that the emails “appear to be pertinent” to the investigation of Mrs. Clinton’s use of a private server.
## 2 Mrs. Clinton’s campaign opened a coordinated attack on Mr. Comey, the F.B.I. director, accusing him of smearing her with innuendo late in the race.
## 3 Here’s what you need to know about the week’s top stories.
## 4 Higher yields with less pesticides was the sales pitch for genetically modified seeds. But that has not proved to be the outcome in the United States.
## 5 If the announcement influences the election, it could be a violation of the Hatch Act.
## 6 Senior officials warned the F.B.I. director on policies against talking about current criminal investigations or being seen as meddling in elections.
## published_date source asset_id total_shares
## 1 2016-10-28 The New York Times 1e+14 1
## 2 2016-10-29 The New York Times 1e+14 2
## 3 2016-10-30 The New York Times 1e+14 3
## 4 2016-10-29 The New York Times 1e+14 4
## 5 2016-10-30 The New York Times 1e+14 5
## 6 2016-10-29 The New York Times 1e+14 6
## des_facet
## 1 PRESIDENTIAL ELECTION OF 2016, CLASSIFIED INFORMATION AND STATE SECRETS, E-MAIL
## 2 PRESIDENTIAL ELECTION OF 2016, CLASSIFIED INFORMATION AND STATE SECRETS
## 3
## 4 GENETICALLY MODIFIED FOOD (GMOS), BIOTECHNOLOGY AND BIOENGINEERING
## 5 PRESIDENTIAL ELECTION OF 2016, E-MAIL, CLASSIFIED INFORMATION AND STATE SECRETS
## 6 PRESIDENTIAL ELECTION OF 2016, CLASSIFIED INFORMATION AND STATE SECRETS
## org_facet
## 1 FEDERAL BUREAU OF INVESTIGATION
## 2 FEDERAL BUREAU OF INVESTIGATION
## 3
## 4 BAYER AG, DOW CHEMICAL COMPANY, EUROPEAN UNION, SYNGENTA AG, MONSANTO COMPANY, AGRICULTURE DEPARTMENT
## 5 FEDERAL BUREAU OF INVESTIGATION, HOUSE OF REPRESENTATIVES, SENATE
## 6 FEDERAL BUREAU OF INVESTIGATION, JUSTICE DEPARTMENT
## per_facet
## 1 CLINTON, HILLARY RODHAM, COMEY, JAMES B, WEINER, ANTHONY D
## 2 CLINTON, HILLARY RODHAM, COMEY, JAMES B, TRUMP, DONALD J
## 3
## 4
## 5 CLINTON, HILLARY RODHAM, COMEY, JAMES B
## 6 CLINTON, HILLARY RODHAM, COMEY, JAMES B, ABEDIN, HUMA, WEINER, ANTHONY D
## geo_facet
## 1
## 2
## 3
## 4
## 5
## 6
## media
## 1 image, photo, Huma Abedin and Hillary Clinton on the campaign’s plane on Friday., Doug Mills/The New York Times, https://static01.nyt.com/images/2016/10/29/us/29EMAILSwebsub/29EMAILSwebsub-thumbStandard-v2.jpg, https://static01.nyt.com/images/2016/10/29/us/29EMAILSwebsub/29EMAILSwebsub-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2016/10/29/us/29EMAILSwebsub/29EMAILSwebsub-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 2 image, photo, Hillary Clinton in Des Moines on Friday. Her advisers said they were not changing her strategy or schedule because of the emails., Doug Mills/The New York Times, https://static01.nyt.com/images/2016/10/30/us/30CAMPAIGN/30CAMPAIGN-thumbStandard-v2.jpg, https://static01.nyt.com/images/2016/10/30/us/30CAMPAIGN/30CAMPAIGN-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2016/10/30/us/30CAMPAIGN/30CAMPAIGN-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 3 image, photo, NA, Doug Mills/The New York Times, https://static01.nyt.com/images/2016/10/29/briefing/29briefing-weekend-slide-4P0Z/29briefing-weekend-slide-4P0Z-thumbStandard.jpg, https://static01.nyt.com/images/2016/10/29/briefing/29briefing-weekend-slide-4P0Z/29briefing-weekend-slide-4P0Z-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2016/10/29/briefing/29briefing-weekend-slide-4P0Z/29briefing-weekend-slide-4P0Z-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 4 image, photo, Arnaud Rousseau, a sixth-generation farmer in France, in a field of rapeseed. Twenty years ago, Europe largely rejected genetic modification at the same time the United States and Canada were embracing it., Ed Alcock for The New York Times, https://static01.nyt.com/images/2016/09/16/business/GMO1/00CHEMCROP1-thumbStandard.jpg, https://static01.nyt.com/images/2016/09/16/business/GMO1/GMO1-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2016/09/16/business/GMO1/GMO1-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 5 image, photo, James Comey, director of the Federal Bureau of Investigation, testifying during a House Judiciary Committee hearing in March., T.J. Kirkpatrick for The New York Times, https://static01.nyt.com/images/2016/10/30/opinion/30painter-img/30painter-img-thumbStandard.jpg, https://static01.nyt.com/images/2016/10/30/opinion/30painter-img/30painter-img-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2016/10/30/opinion/30painter-img/30painter-img-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 6 image, photo, James B. Comey, the F.B.I. director, in July. His letter to Congress on Friday opened him to fierce criticism., Al Drago/The New York Times, https://static01.nyt.com/images/2016/10/30/us/30FBI/30FBI-thumbStandard.jpg, https://static01.nyt.com/images/2016/10/30/us/30FBI/30FBI-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2016/10/30/us/30FBI/30FBI-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
The data NYTMostPopular can now be used just like any other data frame in R.