.scroll-100 {
  max-height: 400px;
  overflow-y: auto;
  overflow-x: auto;
}
The New York Times provides a rich set of APIs for accessing their content. In this assignment, I will use the Top Stories API to retrieve the most important or currently featured articles from the Science section. I will construct an interface in R to read JSON data from the API and transform it into an R DataFrame for analysis.
API Documentation: https://developer.nytimes.com/apis
library(httr)
library(jsonlite)
library(knitr)
library(DT)
library(DT)
Here I’m requesting the top stories from the Science section of the New York Times.
# Set API key
api_key <- "CMlUm9FC6ifPzWmf05n0ZS0l4t5PgTef"
# Make GET request to Top Stories API
response <- GET(
  "https://api.nytimes.com/svc/topstories/v2/science.json",
  query = list(`api-key` = api_key)
)
# Check if request was successful
if (status_code(response) == 200) {
  cat("API request successful!\n")
} else {
  stop(paste("API request failed with status:", status_code(response)))
}
## API request successful!
# Parse JSON response
json_text <- content(response, "text", encoding = "UTF-8")
data <- fromJSON(json_text)
# Convert to dataframe
articles_df <- as.data.frame(data$results)
cat("Number of articles retrieved:", nrow(articles_df))
## Number of articles retrieved: 27
datatable(
  articles_df[, c("title", "abstract", "published_date", "byline")],
  options = list(
    pageLength = 5,
    scrollX = TRUE,
    scrollY = "300px",
    scrollCollapse = TRUE,
    paging = TRUE
  ),
  class = 'cell-border stripe',
  rownames = FALSE
)
# Display available columns
cat("Available columns:\n")
## Available columns:
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"
# Check dimensions
cat("\nDataset dimensions:", nrow(articles_df), "rows x", ncol(articles_df), "columns")
## 
## Dataset dimensions: 27 rows x 19 columns
# Data structure
#str(articles_df)
#First few rows
head(articles_df, 2)
##   section subsection                                    title
## 1 science            Sign Up for the Science Times Newsletter
## 2   admin                                                    
##                                                                                                 abstract
## 1 Every week, we’ll bring you stories that capture the wonders of the human body, nature and the cosmos.
## 2                                                                                                       
##    url                                                            uri byline
## 1 null nyt://embeddedinteractive/daba9d03-29b8-5cfc-8cbd-74b8abf54a07       
## 2      nyt://embeddedinteractive/6cb185ed-65ac-50a5-ace3-5bab81934245       
##             item_type              updated_date              created_date
## 1 EmbeddedInteractive 2018-04-07T13:23:25-04:00 2016-02-05T18:18:53-05:00
## 2 EmbeddedInteractive 2015-07-20T13:45:03-04:00 2015-04-16T14:13:18-04:00
##              published_date material_type_facet kicker des_facet org_facet
## 1 2016-02-05T18:18:53-05:00                                               
## 2 2015-04-16T14:13:18-04:00                                               
##   per_facet geo_facet
## 1                    
## 2                    
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  multimedia
## 1 https://static01.nyt.com/images/2016/02/06/science/sciencenewsletter2/sciencenewsletter2-superJumbo.jpg, https://static01.nyt.com/images/2016/02/06/science/sciencenewsletter2/sciencenewsletter2-mediumThreeByTwo440.jpg, https://static01.nyt.com/images/2016/02/06/science/sciencenewsletter2/sciencenewsletter2-thumbLarge.jpg, Super Jumbo, mediumThreeByTwo440, Large Thumbnail, 1534, 293, 150, 2048, 440, 150, image, image, image, photo, photo, photo, , , , NASA, via Associated Press, NASA, via Associated Press, NASA, via Associated Press
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
##   short_url
## 1          
## 2
# Display first few articles with key information
kable(head(articles_df[, c("title", "abstract", "published_date", "byline")], 5),
      caption = "Top 10 Science Stories from NYT")
| title | abstract | published_date | byline | 
|---|---|---|---|
| Sign Up for the Science Times Newsletter | Every week, we’ll bring you stories that capture the wonders of the human body, nature and the cosmos. | 2016-02-05T18:18:53-05:00 | |
| 2015-04-16T14:13:18-04:00 | |||
| How China Raced Ahead of the U.S. on Nuclear Power | The United States was once the undisputed leader in atomic energy. Now it is trying to catch up. | 2025-10-22T22:00:01-04:00 | |
| Iceland Announces an Unfortunate First: Mosquitoes | Iceland was one of the only mosquito-free places in the world, at least according to its records. Not anymore. | 2025-10-22T16:47:41-04:00 | By Amelia Nierenberg | 
| Congress Members Question Pentagon’s Delay in ‘Forever Chemical’ Cleanup | A bipartisan group of lawmakers has asked the military to explain why cleanup of PFAS chemicals at bases nationwide has been pushed back. | 2025-10-22T15:45:43-04:00 | By Hiroko Tabuchi | 
# Show details of the first article
cat("First Article Title:", articles_df$title[1], "\n\n")
## First Article Title: Sign Up for the Science Times Newsletter
cat("Abstract:", articles_df$abstract[1], "\n\n")
## Abstract: Every week, we’ll bring you stories that capture the wonders of the human body, nature and the cosmos.
cat("URL:", articles_df$url[1], "\n\n")
## URL: null
cat("Published:", articles_df$published_date[1])
## Published: 2016-02-05T18:18:53-05:00
This assignment successfully demonstrated how to access the New York Times Top Stories API and transform JSON data into a structured R DataFrame. I retrieved 27 current science articles, each containing key information such as titles, abstracts, URLs, publication dates, bylines, and multimedia elements. The resulting DataFrame is ready for further analysis, visualization, or storage in a local database.
# Save to CSV for future use
library(readr)
# This will convert list columns to strings
write_csv(articles_df, "nyt_science_articles.csv")