The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis You’ll need to start by signing up for an API key. Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it into an R DataFrame:
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(httr)
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
library(dotenv)
#load the api key in .env file
load_dot_env()
api_key <- Sys.getenv("api_key")
#set period separately to make it easy to adjust
period <- 7
url <- sprintf("https://api.nytimes.com/svc/mostpopular/v2/viewed/%d.json", period)
response <- GET(url, query = list(`api-key` = api_key))
#check status
if (http_status(response)$category == "Success") {
# Parse the JSON content
data <- content(response, "text") %>%
fromJSON(flatten = TRUE)
} else {
#print error
stop("Error: Unable to retrieve data from the API.")
}
#convert to dataframe
df <- as.data.frame(data)
Analyses
I had some basic questions I wanted to collect from the list of most viewed articles I collected. I asked and plotted their answers.
#plot the NYT section the top 20 articles appear in
ggplot(df, aes(x = results.section)) +
geom_bar(fill = "skyblue", color = "black") +
labs(title = "Frequency of Sections", x = "Section", y = "Frequency") +
theme_minimal() +
coord_flip()
#plot the frequency of specific keywords
df <- df %>%
mutate(keyword_appearance = grepl("Gaza|Israel", results.adx_keywords, ignore.case = TRUE))
#create a barplot of the frequency of rows with 'Gaza' or 'Israel' in the keywords
ggplot(df, aes(x = factor(keyword_appearance), fill = factor(keyword_appearance))) +
geom_bar() +
labs(title = "Frequency of 'Gaza' or 'Israel' in Keywords", x = "Israel/Gaza", y = "Frequency") +
scale_x_discrete(labels = c("Does Not Appear", "Appears")) +
scale_fill_manual(values = c("gray", "blue")) + # Adjust the colors as needed
theme_minimal() +
guides(fill = FALSE)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.