assignment9

Approach

Learn how to create an API key and then access that API key in code. Fetching data from an API and parsing through it.

Code Base

Library

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(stringr)
library(httr2)
library(purrr)

Authentication

Sys.setenv(NYT_API_KEY = "UjAKE8AgetLZdxKXfCsEO9G1vOkKnhQGNotGmKZHRA3knAAb")
api_key <- Sys.getenv("NYT_API_KEY")

if (nchar(api_key) == 0) {
  stop("NYT_API_KEY environment variable is not set.")
}

Fetch Data

fetch_most_viewed <- function(section = "all-sections", period = 30) {
  url <- paste0(
    "https://api.nytimes.com/svc/mostpopular/v2/mostviewed/",
    section, "/", period, ".json"
  )
  request(url) |>
    req_url_query(`api-key` = api_key) |>
    req_perform() |>
    resp_body_json(simplifyVector = FALSE)
}

raw <- fetch_most_viewed()

Into Tiny Data Frame

parse_article <- function(a) {
  tibble(
    title        = a$title          %||% NA_character_,
    section      = a$section        %||% NA_character_,
    byline       = a$byline         %||% NA_character_,
    abstract     = a$abstract       %||% NA_character_,
    published_dt = as.Date(a$published_date %||% NA_character_),
    url          = a$url            %||% NA_character_
  )
}

articles <- map(raw$results, parse_article) |>
  bind_rows() |>
  mutate(byline = str_remove(byline, "^By "))

print(articles)
# A tibble: 20 × 6
   title                              section byline abstract published_dt url  
   <chr>                              <chr>   <chr>  <chr>    <date>       <chr>
 1 Cesar Chavez, a Civil Rights Icon… U.S.    "Mann… An inve… 2026-03-18   http…
 2 How Trump and His Advisers Miscal… U.S.    "Mark… In the … 2026-03-10   http…
 3 Georgia Teacher Is Killed After T… U.S.    "Alex… The tea… 2026-03-08   http…
 4 Kristi Noem Survived Many Crises.… U.S.    "Zola… Preside… 2026-03-06   http…
 5 Kash Patel’s Girlfriend Seeks Fam… U.S.    "Eliz… Former … 2026-02-28   http…
 6 Crossplay Game Review              The Up… ""     Informa… 2025-10-07   http…
 7 Daryl Hannah: How Can ‘Love Story… Opinion "Dary… The cha… 2026-03-06   http…
 8 Robert S. Mueller III, 81, Dies; … U.S.    ""     He impo… 2026-03-21   http…
 9 Alexander Brothers Found Guilty o… New Yo… "Debr… The ver… 2026-03-09   http…
10 Elite Doctors Served Jeffrey Epst… U.S.    "Davi… A small… 2026-02-28   http…
11 Tracking T.S.A. Wait Times at Maj… U.S.    "Emma… Travele… 2026-03-23   http…
12 I Went to Florida to See the 31-Y… Opinion "Mich… James F… 2026-03-12   http…
13 Hegseth Strikes Two Black and Two… U.S.    "Greg… Defense… 2026-03-27   http…
14 I Predicted the 2008 Financial Cr… Opinion "Rich… Richard… 2026-03-16   http…
15 Punching, Slamming, Screaming: A … Food    "Juli… Dozens … 2026-03-07   http…
16 Joe Kent, a Top U.S. Counterterro… U.S.    "Juli… Mr. Ken… 2026-03-17   http…
17 Fire on U.S. Aircraft Carrier Rag… U.S.    "Hele… The For… 2026-03-16   http…
18 F.C.C. Chair Threatens to Revoke … World   "Ashl… The com… 2026-03-14   http…
19 ‘That's When the Boos Started’: T… Magazi… "Inte… Days af… 2026-03-12   http…
20 Trump Administration Begins Inqui… U.S.    "Mich… The Jus… 2026-03-26   http…
articles |>
  count(section, sort = TRUE)
# A tibble: 7 × 2
  section        n
  <chr>      <int>
1 U.S.          12
2 Opinion        3
3 Food           1
4 Magazine       1
5 New York       1
6 The Upshot     1
7 World          1

Conclusion

The API endpoint I selected was the “Most Popular API” from the Ny times developer page. I wanted to explore the question “What is the most popular or most read section in the New York Times newspaper?” Through this API I was able to collect the most popular articles and what category they fall under. I was then able to see the frequency of each articles category and display that data to see the most popular or read section which comes out to be “U.S”. Following that is opinion but U.S is unarguable the most popular.