library(config)
## Do not use `library(config)` to attach the config package.
## Use `config::get()` directly.
## Attaching the config package can lead to namespace conflicts.
##
## Attaching package: 'config'
## The following objects are masked from 'package:base':
##
## get, merge
library(httr)
##
## Attaching package: 'httr'
## The following object is masked from 'package:config':
##
## with_config
library(jsonlite)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
config <- config::get()
api_key <- config$nyt_api_key
base_url <- "https://api.nytimes.com/svc/topstories/v2/"
section <- "home"
endpoint <- paste0(base_url, section, ".json")
query_params <- list(
`api-key` = api_key
)
# error handling
get_nyt_data <- function(url, params) {
tryCatch({
response <- GET(url, query = params)
if (status_code(response) == 429) {
stop("Rate limit exceeded.")
} else if (status_code(response) >= 400) {
error_details <- rawToChar(response$content)
stop("Request failed with code ", status_code(response),
" Error: ", error_details)
} else {
message("Success: HTTP Status Code ", status_code(response))
}
return(response)
}, error = function(err) {
message("Error: ", err$message)
return(NULL)
})
}
response <- get_nyt_data(endpoint, query_params)
## Success: HTTP Status Code 200
if (!is.null(response)) {
# raw response to text
data_text <- rawToChar(response$content)
# Parse JSON file
json_data <- fromJSON(data_text, flatten = TRUE)
# Transform JSON data into R DF
nyt_topstories <- as.data.frame(json_data$results)
print(head(nyt_topstories))
} else {
message("Failed to retrieve data from the API.")
}
## section subsection
## 1 us politics
## 2 us politics
## 3 world europe
## 4 business economy
## 5 world asia
## 6 world asia
## title
## 1 ‘Should I Fire Him?’ Inside Trump’s Deliberations Over the Fate of Michael Waltz
## 2 Trump Order Could Cripple Federal Worker Unions Fighting DOGE Cuts
## 3 No Second Lady, No Problem: Greenland’s Dog Sled Race Goes Barking On
## 4 Specter of Auto Tariffs Spurs Some Car Buyers to Rush Purchases
## 5 Myanmar Earthquake Toll Surpasses 1,600 Dead Amid Search for Survivors
## 6 The gutting of U.S.A.I.D. is likely to hinder recovery efforts in Myanmar.
## abstract
## 1 In public, President Trump has defended his national security adviser. But behind the scenes, he has been casting around for advice.
## 2 The move added to the list of actions by President Trump that use the powers of his office to weaken perceived enemies.
## 3 One of Greenland’s national pastimes was nearly hijacked this week after the White House said the Second Lady would attend. She didn’t. But a lot of people talked about this as the mushers did their work.
## 4 “Prices are going to shoot up now,” one shopper said. But some dealers said that economic concerns might be keeping people away.
## 5 Aid workers delivered the first shipments of help to Myanmar, but will have to cross a country buckled by the disaster and divided by civil war, arms dealers and drug syndicates.
## 6
## url
## 1 https://www.nytimes.com/2025/03/29/us/politics/trump-signal-michael-waltz.html
## 2 https://www.nytimes.com/2025/03/29/us/politics/federal-worker-unions-doge.html
## 3 https://www.nytimes.com/2025/03/29/world/europe/greenland-dog-sled-race-vance-trump.html
## 4 https://www.nytimes.com/2025/03/29/business/economy/auto-tariffs-buyers-dealers.html
## 5 https://www.nytimes.com/2025/03/29/world/asia/myanmar-earthquake.html
## 6 https://www.nytimes.com/live/2025/03/29/world/earthquake-myanmar-thailand/the-gutting-of-usaid-is-likely-to-hinder-recovery-efforts-in-myanmar
## uri
## 1 nyt://article/9c8896cb-5cd8-5a1b-99e3-336692975fa1
## 2 nyt://article/326b3ef0-7797-51f1-a7a0-369016636183
## 3 nyt://article/ee0b1f8d-8148-5b14-b652-ac470a4639f4
## 4 nyt://article/c4deeed7-f4f5-5dc0-9b5b-76762f5f007b
## 5 nyt://article/9ac862af-ced7-510b-8cb5-8eac092c85cf
## 6 nyt://article/ce608fb8-4493-5cd1-b4b2-84b765877144
## byline item_type updated_date
## 1 By Maggie Haberman and Tyler Pager Article 2025-03-29T20:36:34-04:00
## 2 By Rebecca Davis O’Brien Article 2025-03-29T11:08:48-04:00
## 3 By Maya Tekeli and Jeffrey Gettleman Article 2025-03-29T18:03:47-04:00
## 4 By Neal E. Boudette and Mark Bonamo Article 2025-03-29T20:24:01-04:00
## 5 By Sui-Lee Wee and Alan Yuhas Article 2025-03-29T16:48:33-04:00
## 6 By Tim Balk Article 2025-03-29T14:00:34-04:00
## created_date published_date material_type_facet
## 1 2025-03-29T15:23:52-04:00 2025-03-29T15:23:52-04:00
## 2 2025-03-29T10:30:36-04:00 2025-03-29T10:30:36-04:00
## 3 2025-03-29T17:05:56-04:00 2025-03-29T17:05:56-04:00
## 4 2025-03-29T18:07:12-04:00 2025-03-29T18:07:12-04:00
## 5 2025-03-29T14:21:38-04:00 2025-03-29T14:21:38-04:00
## 6 2025-03-29T14:00:34-04:00 2025-03-29T14:00:34-04:00
## kicker
## 1
## 2
## 3 Greenland Dispatch
## 4
## 5
## 6
## des_facet
## 1 Signal Chat Leak (Trump Administration), Classified Information and State Secrets, United States Defense and Military Forces, United States Politics and Government
## 2 United States Politics and Government, Organized Labor, Government Employees, Collective Bargaining, Suits and Litigation (Civil), Executive Orders and Memorandums
## 3 Dog Sledding, United States International Relations
## 4 Automobiles, Prices (Fares, Fees and Rates), Customs (Tariff), Demonstrations, Protests and Riots, Shopping and Retail, Used Cars
## 5 Myanmar Earthquake (March 28, 2025), Earthquakes, Deaths (Fatalities), War and Armed Conflicts, Defense and Military Forces
## 6
## org_facet
## 1 Atlantic Monthly
## 2 American Federation of Government Employees, Government Efficiency Department (US)
## 3
## 4 Tesla Motors Inc
## 5
## 6
## per_facet
## 1 Waltz, Michael (1974- ), Hegseth, Pete, Trump, Donald J, Vance, J D, Goldberg, Jeffrey
## 2
## 3 Vance, Usha, Trump, Donald J, Vance, JD
## 4 Musk, Elon, Trump, Donald J
## 5 Min Aung Hlaing
## 6
## geo_facet
## 1
## 2
## 3 Greenland, Sisimiut, Greenland
## 4 Queens (NYC)
## 5 Mandalay (Myanmar), Myanmar, India, China
## 6
## multimedia
## 1 https://static01.nyt.com/images/2025/03/29/multimedia/29dc-TrumpSignal-topart-kvwg/29dc-TrumpSignal-topart-kvwg-superJumbo.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/29dc-TrumpSignal-topart-kvwg/29dc-TrumpSignal-topart-kvwg-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/29dc-TrumpSignal-topart-kvwg/29dc-TrumpSignal-topart-kvwg-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1365, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, President Trump has been considering whether to fire Michael Waltz, his national security adviser, after it came to light this week that he had created a Signal chat to share sensitive information. , President Trump has been considering whether to fire Michael Waltz, his national security adviser, after it came to light this week that he had created a Signal chat to share sensitive information. , President Trump has been considering whether to fire Michael Waltz, his national security adviser, after it came to light this week that he had created a Signal chat to share sensitive information. , Haiyun Jiang for The New York Times, Haiyun Jiang for The New York Times, Haiyun Jiang for The New York Times
## 2 https://static01.nyt.com/images/2025/03/29/multimedia/02-fbqh/02-fbqh-superJumbo.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/02-fbqh/02-fbqh-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/02-fbqh/02-fbqh-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1365, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, Dozens of members and supporters of the American Federation of Government Employees union rallied outside NASA’s Johnson Space Center in Houston last week., Dozens of members and supporters of the American Federation of Government Employees union rallied outside NASA’s Johnson Space Center in Houston last week., Dozens of members and supporters of the American Federation of Government Employees union rallied outside NASA’s Johnson Space Center in Houston last week., Meridith Kohut for The New York Times, Meridith Kohut for The New York Times, Meridith Kohut for The New York Times
## 3 https://static01.nyt.com/images/2025/03/29/29visualUploader-37701-cover/29visualUploader-37701-cover-superJumbo.jpg, https://static01.nyt.com/images/2025/03/29/29visualUploader-37701-cover/29visualUploader-37701-cover-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/29/29visualUploader-37701-cover/29visualUploader-37701-cover-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 848, 320, 150, 480, 480, 150, image, image, image, photo, photo, photo, , , , , ,
## 4 https://static01.nyt.com/images/2025/03/29/multimedia/29biz-auto-buyers-tmcl/29biz-auto-buyers-tmcl-superJumbo.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/29biz-auto-buyers-tmcl/29biz-auto-buyers-tmcl-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/29biz-auto-buyers-tmcl/29biz-auto-buyers-tmcl-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1365, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, The tariffs, expected to go into effect on April 3, will have a broad impact on the North American auto industry. The 25 percent levies on vehicles and parts produced outside the United States will raise the prices of new vehicles., The tariffs, expected to go into effect on April 3, will have a broad impact on the North American auto industry. The 25 percent levies on vehicles and parts produced outside the United States will raise the prices of new vehicles., The tariffs, expected to go into effect on April 3, will have a broad impact on the North American auto industry. The 25 percent levies on vehicles and parts produced outside the United States will raise the prices of new vehicles., Hilary Swift for The New York Times, Hilary Swift for The New York Times, Hilary Swift for The New York Times
## 5 https://static01.nyt.com/images/2025/03/29/multimedia/29int-myanmar-quake-ledeall-01-cjvp/29int-myanmar-quake-ledeall-01-cjvp-superJumbo.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/29int-myanmar-quake-ledeall-01-cjvp/29int-myanmar-quake-ledeall-01-cjvp-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/29int-myanmar-quake-ledeall-01-cjvp/29int-myanmar-quake-ledeall-01-cjvp-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1365, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, Rescue teams working to free trapped residents at the destroyed Sky Villa Condominium in Mandalay, Myanmar, on Saturday, a day after the earthquake struck., Rescue teams working to free trapped residents at the destroyed Sky Villa Condominium in Mandalay, Myanmar, on Saturday, a day after the earthquake struck., Rescue teams working to free trapped residents at the destroyed Sky Villa Condominium in Mandalay, Myanmar, on Saturday, a day after the earthquake struck., Sai Aung Main/Agence France-Presse — Getty Images, Sai Aung Main/Agence France-Presse — Getty Images, Sai Aung Main/Agence France-Presse — Getty Images
## 6 https://static01.nyt.com/images/2025/03/29/multimedia/29myanmar-earthquake-usaid-01-mktq/29myanmar-earthquake-usaid-01-mktq-superJumbo.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/29myanmar-earthquake-usaid-01-mktq/29myanmar-earthquake-usaid-01-mktq-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2025/03/29/multimedia/29myanmar-earthquake-usaid-01-mktq/29myanmar-earthquake-usaid-01-mktq-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1366, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, In the past, the U.S. Agency for International Development mobilized major relief responses after earthquakes. Not this time., In the past, the U.S. Agency for International Development mobilized major relief responses after earthquakes. Not this time., In the past, the U.S. Agency for International Development mobilized major relief responses after earthquakes. Not this time., Jason Andrew for The New York Times, Jason Andrew for The New York Times, Jason Andrew for The New York Times
## short_url
## 1
## 2
## 3
## 4
## 5
## 6
Most articles (11) do not have a subsection identified, but of the remaining articles that do the “Asia” subsection has the most (3) with “Economy” “Europe” and “Middle East” all having the least (1).
Most writers have 1 article each in this dataframe but Adeel Hassan has the most (2).
table(nyt_topstories$subsection)
##
## asia economy europe middleeast politics
## 12 3 1 1 1 2
table(nyt_topstories$byline)
##
## By Adeel Hassan
## 2
## By Carlotta Gall and Christina Goldbaum
## 1
## By Juliet Macur
## 1
## By Lulu Garcia-Navarro
## 1
## By Maggie Haberman and Tyler Pager
## 1
## By Maia Coleman
## 1
## By Maya Tekeli and Jeffrey Gettleman
## 1
## By Megan Griffiths and Mindie Lind
## 1
## By Muktita Suhartono and Richard C. Paddock
## 1
## By Neal E. Boudette and Mark Bonamo
## 1
## By Nick Keppler
## 1
## By Nicole Acheampong
## 1
## By Rebecca Davis O’Brien
## 1
## By Ross Douthat
## 1
## By Somini Sengupta and Cassandra Klos
## 1
## By Steven Kurutz
## 1
## By Sui-Lee Wee and Alan Yuhas
## 1
## By The New York Times
## 1
## By Tim Balk
## 1
ggplot(nyt_topstories, aes(x = subsection)) +
geom_bar(fill = "lightpink", color = "black") +
labs(title = "Number of Articles per Subsection",
x = "Subsection", y = "Count") +
theme_minimal()