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.
library(jsonlite)
library(stringr)
For this assignment, I’ll be using the NY Times’ Search API to search for any material related to Election published since July 2016 from their Politics news desk / section.
base_url <- "http://api.nytimes.com/svc/search/v2/articlesearch.json?"
q <-"q=Election"
fq <- "&fq=source:(The New York Times) AND news_desk:(Politics)"
s <- "&sort=newest"
bdate <- "&begin_date=20160701"
edate <- "&end_date=20161031"
search_api_key <- "&api-key=e7334ee8a3c24ed38424be8b8a767218"
Construct and submit the URL request
search_url <- URLencode(paste0(base_url, q, fq, s, bdate, edate, search_api_key))
response <- fromJSON(search_url)
Save the JSON response as dataframe
data <- as.data.frame(response)
Display the dataframe with selected and renamed columns
data <- data.frame(
str_sub(data$response.docs.pub_date,1,10),
data$response.docs.news_desk,
data$response.docs.headline,
data$response.docs.web_url,
data$response.docs.byline[,2],
data$response.docs.type_of_material)
names(data) <- c("Date", "Section", "Headline","URL", "Author", "Material")
knitr::kable(data)
| Date | Section | Headline | URL | Author | Material |
|---|---|---|---|---|---|
| 2016-10-23 | U.S. / Politics | ‘Not a Regular Day’: What a Trump Rally Clash Reveals About a Divided Nation | http://www.nytimes.com/interactive/2016/10/23/us/politics/donald-trump-protest-san-jose.html | By FARAH STOCKMAN | Interactive Feature |
| 2016-10-20 | U.S. / Politics | A ‘Taco Wall’ to Protest Trump | http://www.nytimes.com/video/us/politics/100000004719298/a-taco-wall-to-protest-trump.html | By NICK CORASANITI and ERICA BERENSTEIN | Video |
| 2016-10-18 | U.S. / Politics | Trump Wants Big Changes to Legal Immigration, Too — How Big? | http://www.nytimes.com/interactive/2016/10/18/us/politics/trump-legal-immigration.html | By JUGAL K. PATEL | Interactive Feature |
| 2016-10-14 | U.S. / Politics | To Lead or Disrupt? | http://www.nytimes.com/video/us/politics/100000004710213/to-lead-or-disrupt.html | By NATALIA V. OSIPOVA, ELSA BUTLER and ALAN RAPPEPORT | Video |
| 2016-10-14 | U.S. / Politics | Press Tensions at Trump Rallies | http://www.nytimes.com/video/us/politics/100000004709510/trump-supporters-jeer-at-press.html | By AINARA TIEFENTHÄLER and JIM RUTENBERG | Video |
| 2016-10-11 | U.S. / Politics | Where Republicans in Competitive House and Senate Races Stand on Donald Trump | http://www.nytimes.com/interactive/2016/10/11/us/politics/house-and-senate-support-for-trump.html | By KAREN YOURISH, ADAM PEARCE and JASMINE C. LEE | Interactive Feature |
| 2016-10-10 | U.S. / Politics | Full Video: Second Presidential Debate | http://www.nytimes.com/video/us/politics/100000004696519/watch-live-second-presidential-debate.html | By THE NEW YORK TIMES | NA |
| 2016-10-09 | U.S. / Politics | What to Expect From the Second Debate | http://www.nytimes.com/video/us/politics/100000004697367/what-to-expect-from-the-second-debate.html | By SHANE O’NEILL and ALEX BURNS | Video |
| 2016-10-08 | U.S. / Politics | Paul Ryan and Mitch McConnell Reject Donald Trump’s Words, Over and Over, but Not His Candidacy | http://www.nytimes.com/interactive/2016/10/08/us/politics/how-paul-ryan-and-mitch-mcconnell-have-disavowed-trumps-words-but-not-their-support.html | By LARRY BUCHANAN, ALICIA PARLAPIANO and KAREN YOURISH | Interactive Feature |
| 2016-10-07 | U.S. / Politics | On the Trail With Gary Johnson | http://www.nytimes.com/interactive/2016/10/07/us/politics/gary-johnson-campaign.html | Photographs by SAM HODGSON | Interactive Feature |