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.
library(jsonlite)
##
## Attaching package: 'jsonlite'
## The following object is masked from 'package:utils':
##
## View
library(knitr)
## Warning: package 'knitr' was built under R version 3.2.3
To request for NY Times API key, register at link
The information about creating query and url to be used can be found here
Web API and URL
api_key <- "&api-key=e2ecd4b8ba0d275c1ae6a42a808a991e:5:74859716"
url <- "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Clinton-Trump"
# the query Clinton-Trump was used by me. We can choose different terms and further refine the queries
dat <- (paste0(url, api_key))
dat <-fromJSON(dat)
#converting dat into a dataframe
df <- as.data.frame(dat$response)
names(df)
## [1] "meta.hits" "meta.time"
## [3] "meta.offset" "docs.web_url"
## [5] "docs.snippet" "docs.lead_paragraph"
## [7] "docs.abstract" "docs.print_page"
## [9] "docs.blog" "docs.source"
## [11] "docs.multimedia" "docs.headline"
## [13] "docs.keywords" "docs.pub_date"
## [15] "docs.document_type" "docs.news_desk"
## [17] "docs.section_name" "docs.subsection_name"
## [19] "docs.byline" "docs.type_of_material"
## [21] "docs._id" "docs.word_count"
## [23] "docs.slideshow_credits"
# creating data frame for the required terms
dataframe <- data.frame(df$docs.section_name,df$docs.lead_paragraph, df$docs.abstract, df$docs.web_url)
kable(dataframe)
| df.docs.section_name | df.docs.lead_paragraph | df.docs.abstract | df.docs.web_url |
|---|---|---|---|
| Blogs | NA | The Real Secret to Avoiding Corporate Taxes | US Foods Plans to Go Public | Trump and Sanders Win New Hampshire Primaries | http://news.blogs.nytimes.com/2016/02/10/morning-agenda-low-oil-prices-and-a-reckoning-on-debt/ |
| U.S. | At a campaign stop in La Crosse, Wis. on Tuesday, the Democratic presidential candidate Hillary Clinton responded to questions about the Republican presidential candidate Donald J. Trump, calling him a “political arsonist.” | NA | http://www.nytimes.com/video/us/politics/100000004300007/clinton-trump-a-political-arsonist.html |
| U.S. | Republican lawmakers are trying to distance themselves from Mr. Trump, while Democrats seize on the chance to run against a candidate who has offended huge sections of the electorate. | Democrats and Republicans are racing to gauge impact of Donald J Trump’s increasingly likely presidential nomination will have on other elections this fall; Trump’s unpredictability and fiery rhetoric has Republican lawmakers trying to distance themselves from him and Democrats hopeful that they will benefit in general election. | http://www.nytimes.com/2016/03/29/us/politics/donald-trumps-success-upends-battle-for-control-of-congress.html |
| U.S. | The Democratic presidential front-runner, Hillary Clinton, said the “bluster, bigotry and bullying” from her Republican counterpart, Donald J. Trump, is “disturbing.” | NA | http://www.nytimes.com/video/us/politics/100000004272506/in-north-carolina-clinton-bashes-trump.html |
| U.S. | Democratic presidential candidates Hillary Clinton and Bernie Sanders are tussling in Wisconsin over who would best represent the Democratic Party against Republican front-runner Donald Trump. | NA | http://www.nytimes.com/reuters/2016/04/02/us/politics/02reuters-usa-election-democrats.html |
| U.S. | Under a fresh cloud of overseas violence, Donald Trump and Hillary Clinton traded wins with their chief rivals on Tuesday and attacked each other’s worldviews as the 2016 presidential contest turned into a clash of would-be commanders in chief. | NA | http://www.nytimes.com/aponline/2016/03/22/us/politics/ap-us-2016-election-rdp.html |
| U.S. | Donald J. Trump and Hillary Clintons resounding triumphs on Tuesday masked a profound, historic and unusual reality: Most Americans still dont like him. Or her. | News Analysis; Democrats and Republicans are being forced to reckon with public distaste for presidential front-runners Hillary Clinton and Donald J Trump, even as candidates gain more support and amass more delegates; large part of electorate has already formed fixed, and often negative, opinion about Trump and Clinton following their decades spent in public eye. | http://www.nytimes.com/2016/03/16/us/politics/hillary-clinton-donald-trump.html |
| Opinion | Elections are not always between a dream candidate and a dreaded one. Sometimes theyre between common sense and catastrophe. | NA | http://www.nytimes.com/2016/03/31/opinion/campaign-stops/bernie-or-bust-is-bonkers.html |
| U.S. | Donald Trump and Hillary Clinton prevailed in Arizona’s primary, putting them a step closer to wrapping things up and getting on with a head-to-head race for the White House, unobstructed by rivals who just won’t quit. | NA | http://www.nytimes.com/aponline/2016/03/22/us/politics/ap-us-2016-election-news-guide.html |
| Magazine | The Republican strategist on why his partys front-runner is a crude lunatic but continues to lead in the polls. | NA | http://www.nytimes.com/2016/03/27/magazine/rick-wilson-would-take-clinton-over-trump.html |