if (!requireNamespace("httr", quietly = TRUE)) {
install.packages("httr", repos="https://cran.rstudio.com/", dependencies = FALSE)
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
install.packages("jsonlite", repos="https://cran.rstudio.com/", dependencies = FALSE)
}
library(httr)
library(jsonlite)
api_key <- "E83YDkBIafML6q7DA59IP5IjIpavp230"
section <- "home"
url <- paste0("https://api.nytimes.com/svc/topstories/v2/", section, ".json?api-key=", api_key)
response <- httr::GET(url)
if (httr::status_code(response) != 200) {
stop(paste0("Failed to retrieve data. HTTP status: ", httr::status_code(response)))
} else {
print(paste0("Request successful! Status code: ", httr::status_code(response)))
}
## [1] "Request successful! Status code: 200"
Next, we'll parse the JSON response and extract the articles:
data_list <- fromJSON(rawToChar(response$content))
articles <- data_list$results
str(articles, max.level = 1)
## 'data.frame': 35 obs. of 19 variables:
## $ section : chr "business" "business" "business" "nyregion" ...
## $ subsection : chr "economy" "" "" "" ...
## $ title : chr "Countries Targeted by Trump’s Tariffs May Strike Back at U.S. Services" "In Trump’s Fight With Perkins Coie, the Richest Firms Are Staying Quiet" "Another Big Law Firm Reaches Agreement With Trump" "Granddaughters of a Paul Weiss Patriarch Deplore the Firm’s Trump Deal" ...
## $ abstract : chr "Service sectors make up the vast bulk of the American economy, which gives trading partners some clout in trade negotiations." "None of the nation’s top-10 firms by revenue have signed a legal brief demonstrating support for the law firm t"| __truncated__ "Milbank, based in Manhattan, agreed to provide $100 million in pro bono legal services to causes supported by t"| __truncated__ "The law firm’s chairman, Brad Karp, capitulated to the president’s threats. The descendants of the man who wrot"| __truncated__ ...
## $ url : chr "https://www.nytimes.com/2025/04/02/business/economy/tariffs-foreign-goods-tariffs-us-services.html" "https://www.nytimes.com/2025/04/02/business/trump-perkins-coie-amicus-brief.html" "https://www.nytimes.com/2025/04/02/business/trump-law-firms-milbank-deal.html" "https://www.nytimes.com/2025/04/02/nyregion/paul-weiss-granddaughters-letter.html" ...
## $ uri : chr "nyt://article/74d32d34-0ba2-54dd-b631-e7ea9300fe7e" "nyt://article/42be7a83-75b8-5a05-88bf-cb6bdae4e385" "nyt://article/9a0ea3eb-0656-5d86-84f0-2c74d2f2ef14" "nyt://article/9731fd61-779b-556e-a7df-5e8d68a700ce" ...
## $ byline : chr "By Patricia Cohen, Adam Satariano, Eshe Nelson and Jeanna Smialek" "By Ben Protess" "By Matthew Goldstein" "By Benjamin Weiser" ...
## $ item_type : chr "Article" "Article" "Article" "Article" ...
## $ updated_date : chr "2025-04-02T17:18:49-04:00" "2025-04-02T19:36:10-04:00" "2025-04-02T17:04:35-04:00" "2025-04-02T16:16:59-04:00" ...
## $ created_date : chr "2025-04-02T16:53:12-04:00" "2025-04-02T12:18:30-04:00" "2025-04-02T16:54:08-04:00" "2025-04-02T12:03:52-04:00" ...
## $ published_date : chr "2025-04-02T16:53:12-04:00" "2025-04-02T12:18:30-04:00" "2025-04-02T16:54:08-04:00" "2025-04-02T12:03:52-04:00" ...
## $ material_type_facet: chr "" "" "" "" ...
## $ kicker : chr "" "" "" "" ...
## $ des_facet :List of 35
## $ org_facet :List of 35
## $ per_facet :List of 35
## $ geo_facet :List of 35
## $ multimedia :List of 35
## $ short_url : chr "" "" "" "" ...
articles_df <- as.data.frame(articles, stringsAsFactors = FALSE)
colnames(articles_df)
## [1] "section" "subsection" "title"
## [4] "abstract" "url" "uri"
## [7] "byline" "item_type" "updated_date"
## [10] "created_date" "published_date" "material_type_facet"
## [13] "kicker" "des_facet" "org_facet"
## [16] "per_facet" "geo_facet" "multimedia"
## [19] "short_url"
simple_df <- articles_df[, c("section", "title", "abstract", "byline", "published_date", "url")]
simple_df$published_date <- format(as.POSIXct(simple_df$published_date), "%B %d, %Y")
head(simple_df)
## section
## 1 business
## 2 business
## 3 business
## 4 nyregion
## 5 us
## 6 us
## title
## 1 Countries Targeted by Trump’s Tariffs May Strike Back at U.S. Services
## 2 In Trump’s Fight With Perkins Coie, the Richest Firms Are Staying Quiet
## 3 Another Big Law Firm Reaches Agreement With Trump
## 4 Granddaughters of a Paul Weiss Patriarch Deplore the Firm’s Trump Deal
## 5 For Trump, Musk Is a Scapegoat and a Liability. But for Now, He’s Still Useful.
## 6 Wisconsin Republicans Hit Their Vote Target, but Democrats Blew Past Theirs
## abstract
## 1 Service sectors make up the vast bulk of the American economy, which gives trading partners some clout in trade negotiations.
## 2 None of the nation’s top-10 firms by revenue have signed a legal brief demonstrating support for the law firm that is resisting an executive order.
## 3 Milbank, based in Manhattan, agreed to provide $100 million in pro bono legal services to causes supported by the president and the firm.
## 4 The law firm’s chairman, Brad Karp, capitulated to the president’s threats. The descendants of the man who wrote its high-minded principles told Mr. Karp that he had betrayed them.
## 5 Elon Musk has become a valuable heat shield for a president who avoids blame at any cost.
## 6 The liberal candidate in the state’s Supreme Court race benefited from outsize Democratic turnout as counties swung left across the state.
## byline
## 1 By Patricia Cohen, Adam Satariano, Eshe Nelson and Jeanna Smialek
## 2 By Ben Protess
## 3 By Matthew Goldstein
## 4 By Benjamin Weiser
## 5 By Jonathan Swan, Maggie Haberman and Theodore Schleifer
## 6 By Reid J. Epstein
## published_date
## 1 April 02, 2025
## 2 April 02, 2025
## 3 April 02, 2025
## 4 April 02, 2025
## 5 April 02, 2025
## 6 April 02, 2025
## url
## 1 https://www.nytimes.com/2025/04/02/business/economy/tariffs-foreign-goods-tariffs-us-services.html
## 2 https://www.nytimes.com/2025/04/02/business/trump-perkins-coie-amicus-brief.html
## 3 https://www.nytimes.com/2025/04/02/business/trump-law-firms-milbank-deal.html
## 4 https://www.nytimes.com/2025/04/02/nyregion/paul-weiss-granddaughters-letter.html
## 5 https://www.nytimes.com/2025/04/02/us/politics/trump-musk-wisconsin.html
## 6 https://www.nytimes.com/2025/04/02/us/politics/wisconsin-turnout-democrats-republicans.html
image_df <- data.frame(
title = articles_df$title,
image_url = sapply(articles_df$multimedia, function(x) {
if(length(x$url) > 0) {
return(x$url[1]) # Get the first image URL
} else {
return(NA)
}
}),
stringsAsFactors = FALSE
)
head(image_df)
## title
## 1 Countries Targeted by Trump’s Tariffs May Strike Back at U.S. Services
## 2 In Trump’s Fight With Perkins Coie, the Richest Firms Are Staying Quiet
## 3 Another Big Law Firm Reaches Agreement With Trump
## 4 Granddaughters of a Paul Weiss Patriarch Deplore the Firm’s Trump Deal
## 5 For Trump, Musk Is a Scapegoat and a Liability. But for Now, He’s Still Useful.
## 6 Wisconsin Republicans Hit Their Vote Target, but Democrats Blew Past Theirs
## image_url
## 1 https://static01.nyt.com/images/2025/04/02/multimedia/02biz-digital-services-vthm/02biz-digital-services-vthm-superJumbo.jpg
## 2 https://static01.nyt.com/images/2025/04/01/multimedia/01BIZ-LAWFIRM-AMICUS-kjqw/01BIZ-LAWFIRM-AMICUS-kjqw-superJumbo.jpg
## 3 https://static01.nyt.com/images/2025/04/02/us/politics/02trump-news-milbank/02trump-news-milbank-superJumbo.jpg
## 4 https://static01.nyt.com/images/2025/04/02/multimedia/02met-paul-weiss1-qvmc/02met-paul-weiss1-qvmc-superJumbo.jpg
## 5 https://static01.nyt.com/images/2025/04/02/multimedia/02dc-trump-musk-01-fmjc/02dc-trump-musk-01-fmjc-superJumbo.jpg
## 6 https://static01.nyt.com/images/2025/04/02/multimedia/2025-04-02-wi-election-embeds-index/2025-04-02-wi-election-embeds-index-superJumbo-v5.png
section_counts <- table(articles_df$section)
section_counts
##
## arts briefing business health movies nyregion opinion
## 1 1 5 2 4 3 2
## style technology us weather well world
## 2 1 9 1 2 2
barplot(section_counts,
main = "Number of Articles by Section",
xlab = "Section",
ylab = "Count",
col = "steelblue",
las = 2)
httr
jsonlite