It returns the 20 most-viewed articles on NYTimes.com over a period of 1, 7, or 30 days. Each article comes with its title, section, byline, publish date, topic tags, and images.
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(purrr)library(tidyr)library(tibble)library(ggplot2)api_key <-Sys.getenv("NYT_API_KEY")if (api_key =="") {stop("NYT_API_KEY is not set. Add it to .Renviron and restart R.")}
The API key is read from my .Renviron file, so it does not appear anywhere in this document.
Pulling the data
resp <-request("https://api.nytimes.com/svc/mostpopular/v2/viewed/30.json") |>req_url_query(`api-key`= api_key) |>req_error(is_error = \(r) FALSE) |>req_perform()if (resp_status(resp) !=200) {stop("Request failed with status ", resp_status(resp))}resp_status(resp)
# A tibble: 10 × 2
rank topic
<int> <chr>
1 1 Murders, Attempted Murders and Homicides
2 1 Domestic Violence
3 2 Midterm Elections (2026)
4 2 Elections, Senate
5 2 Elections, Governors
6 2 United States Politics and Government
7 3 Sex Crimes
8 3 Fraternities and Sororities
9 3 Colleges and Universities
10 3 Suits and Litigation (Civil)
topics has one row for each topic tag on each article.
Cleaning notes
Nested fields:media is a list of images, and each image has its own list of sizes. I kept only the caption of the first image. The topic tags (des_facet) are also a list inside each article, so I put them in a separate long table instead of cramming them into one column.
Missing values: Most articles have an empty subsection, and some have no byline or no image. I changed empty text to NA and kept every article.
Internal tags: A few articles carry tags that start with internal-. These are NYT website settings, not topics, so I removed them.
Types and text: I converted published_date from text to a date and removed “By” from the bylines.
Fields left out: IDs, adx_keywords, and the people, place, and organization tags, since they don’t relate to the question.
The U.S. section had the most articles in the top 20, with 10 of 20, followed by Opinion with 4. The most common topic tag was “United States Politics and Government,” which appeared on 9 articles. The list changes every day, so these results describe the 30 days before October 26, 2026.