Data 607 Week 9 Assignment

Install and Load Libraries

I have commented out the installation as it become redundant after the install.

#install.packages("httr")
#install.packages("jsonlite")

library(tidyverse)
library(dplyr)
library(httr)
library(jsonlite)

Set API Key and Base URL

api_key <- "GyoPqrc00E4mGbiF536bxMRxttlOW1bK"
section <- "home"
url <- paste0("https://api.nytimes.com/svc/topstories/v2/", section, ".json?api-key=", api_key)

Make the API Request

response <- GET(url)
data_json <- content(response, as = "text", encoding = "UTF-8")
data_parsed <- fromJSON(data_json, flatten = TRUE)

Convert to Dataframe and Display

# The main articles are in the "results" field
articles_df <- data_parsed$results

# Clean and preview selected columns
articles_df_clean <- articles_df %>%
  select(title, byline, published_date, url)

knitr::kable(head(articles_df_clean), caption = "Preview of Top Stories")
Preview of Top Stories
title byline published_date url
Now Europe Knows What Trump’s Team Calls It Behind Its Back: ‘Pathetic’ By Jeanna Smialek and Steven Erlanger 2025-03-25T10:34:03-04:00 https://www.nytimes.com/2025/03/25/world/europe/signal-jeffrey-goldberg-message-hegseth.html
Consumer Confidence Is Tumbling as Financial Concerns Mount By Danielle Kaye 2025-03-25T16:09:28-04:00 https://www.nytimes.com/2025/03/25/business/consumer-confidence-trump.html
Russia and Ukraine Agree to Stop Fighting in Black Sea, White House Says By Constant Méheut and Ivan Nechepurenko 2025-03-25T11:54:40-04:00 https://www.nytimes.com/2025/03/25/world/europe/russia-ukraine-deal-black-sea.html
In Aurora, Colo., a Split Over the Biggest Threat to the City: Migrants or Trump? By Jack Healy and Tim Arango 2025-03-25T13:21:58-04:00 https://www.nytimes.com/2025/03/25/us/politics/trump-aurora-migrants.html
Court Lets Trump Pause New Refugee Admissions but Thousands Must Be Let In By Mattathias Schwartz 2025-03-25T16:35:06-04:00 https://www.nytimes.com/2025/03/25/us/politics/trump-refugees.html
Judge Orders U.S. to Stop Attempts to Deport Columbia Undergraduate By Santul Nerkar and Jonah E. Bromwich 2025-03-25T16:25:35-04:00 https://www.nytimes.com/2025/03/25/nyregion/columbia-university-protester-chung-deportation.html

Conclusion

I have to admit that I’ve spent 15 years telling developers to “pull some API data” as a part of the web development process as a Creative Director. I functionally understood the concept of what it meant to call an API and use it to dynamically provide data. However, this is the first time I’ve ever actually done the work myself. I give myself an internet high five.