Intoduction.
For this week’s assignment, we need to choose one APIs from the New
York Times web page, get an API key, construct an interface in R to read
the JSON data, and and transform it into an R DataFrame.
Import Libraries required for this assignment.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ 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(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
##
## Attaching package: 'plyr'
##
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
##
## The following object is masked from 'package:purrr':
##
## compact
library(dplyr)
library(httr2)
library(knitr)
Get APIs and keys from NYT section “Most Popular by Email” into
R.
emailed_url <- fromJSON("https://api.nytimes.com/svc/mostpopular/v2/emailed/1.json?api-key=w8OnkqtBI6BsPbTAlCiAyQNJ0LCCRM7F", flatten = TRUE)
Create a DafaFrame of 4 columns.
emailed_df <- ldply(emailed_url[4], data.frame)
Check names within the new DataFrame.
names(emailed_df)
## [1] ".id" "uri" "url" "id"
## [5] "asset_id" "source" "published_date" "updated"
## [9] "section" "subsection" "nytdsection" "adx_keywords"
## [13] "column" "byline" "type" "title"
## [17] "abstract" "des_facet" "org_facet" "per_facet"
## [21] "geo_facet" "media" "eta_id"
Select data to work with, and display it in console.
MostEmailed <- emailed_df %>% select(url, title, section, byline, published_date)
head(MostEmailed)
## url
## 1 https://www.nytimes.com/2024/03/23/books/booksupdate/james-mcbride-heaven-and-earth-grocery-store.html
## 2 https://www.nytimes.com/2024/03/20/well/live/aging-biology-dna.html
## 3 https://www.nytimes.com/2024/03/20/arts/downton-abbey-movie.html
## 4 https://www.nytimes.com/2024/03/22/opinion/israel-american-jews-zionism.html
## 5 https://www.nytimes.com/2024/03/22/realestate/tiny-apartment-nyc-400-square-foot.html
## 6 https://www.nytimes.com/article/best-tv-shows-netflix.html
## title
## 1 His Novel Sold a Million Copies. James McBride Isn’t Sure How He Feels About That.
## 2 Why Do We Age?
## 3 A Third ‘Downton Abbey’ Movie Is Coming, One of Its Stars Says
## 4 The Great Rupture in American Jewish Life
## 5 A 400-Square-Foot Apartment That’s Constantly Transforming
## 6 The 50 Best TV Shows on Netflix Right Now
## section byline published_date
## 1 Books By Elizabeth A. Harris 2024-03-23
## 2 Well By Dana G. Smith 2024-03-20
## 3 Arts By Remy Tumin 2024-03-20
## 4 Opinion By Peter Beinart 2024-03-22
## 5 Real Estate By Tim McKeough 2024-03-22
## 6 Arts By Noel Murray 2020-09-04
as.numeric(MostEmailed$published_date)
## Warning: NAs introduced by coercion
## [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
MostEmailed
## url
## 1 https://www.nytimes.com/2024/03/23/books/booksupdate/james-mcbride-heaven-and-earth-grocery-store.html
## 2 https://www.nytimes.com/2024/03/20/well/live/aging-biology-dna.html
## 3 https://www.nytimes.com/2024/03/20/arts/downton-abbey-movie.html
## 4 https://www.nytimes.com/2024/03/22/opinion/israel-american-jews-zionism.html
## 5 https://www.nytimes.com/2024/03/22/realestate/tiny-apartment-nyc-400-square-foot.html
## 6 https://www.nytimes.com/article/best-tv-shows-netflix.html
## 7 https://www.nytimes.com/interactive/2024/03/21/travel/things-to-do-cape-town.html
## 8 https://www.nytimes.com/2024/03/18/books/tommy-orange-there-there-wandering-stars.html
## 9 https://www.nytimes.com/2024/03/22/opinion/dea-opioids-restrictions-overdoses.html
## 10 https://www.nytimes.com/2024/03/22/us/mormon-church-women-latter-day-saints.html
## 11 https://www.nytimes.com/interactive/2024/03/14/travel/things-to-do-sarasota.html
## 12 https://www.nytimes.com/2024/03/20/arts/music/pattie-boyd-george-harrison-eric-clapton-auction.html
## 13 https://www.nytimes.com/2024/03/19/travel/cruise-ships-things-you-dont-know.html
## 14 https://www.nytimes.com/2024/03/22/us/politics/aclu-employee-fired-race-bias.html
## 15 https://www.nytimes.com/2024/03/06/well/move/least-favorite-exercises.html
## 16 https://www.nytimes.com/interactive/2024/03/21/realestate/21hunt-zero.html
## 17 https://www.nytimes.com/2024/03/23/opinion/james-carville-bill-clinton.html
## 18 https://www.nytimes.com/2024/03/23/nyregion/buffalo-total-solar-eclipse.html
## 19 https://www.nytimes.com/2024/03/18/arts/design/katsushika-hokusai-prints-auction.html
## 20 https://www.nytimes.com/2024/03/23/business/jonathan-haidt-smartphones-coddling.html
## title
## 1 His Novel Sold a Million Copies. James McBride Isn’t Sure How He Feels About That.
## 2 Why Do We Age?
## 3 A Third ‘Downton Abbey’ Movie Is Coming, One of Its Stars Says
## 4 The Great Rupture in American Jewish Life
## 5 A 400-Square-Foot Apartment That’s Constantly Transforming
## 6 The 50 Best TV Shows on Netflix Right Now
## 7 36 Hours in Cape Town
## 8 A Bronx Teacher Asked. Tommy Orange Answered.
## 9 The D.E.A. Needs to Stay Out of Medicine
## 10 Does the Mormon Church Empower Women? A Social Media Storm Answers.
## 11 36 Hours in Sarasota, Fla.
## 12 ‘Dear Layla’: Letters for Sale From a Rock Music Love Triangle
## 13 Jail Cells? Morgues? Your Cruise Ship Has Some Surprises for You.
## 14 The A.C.L.U. Said a Worker Used Racist Tropes and Fired Her. But Did She?
## 15 5 Exercises We Hate, and Why You Should Do Them Anyway
## 16 In Santa Barbara, Their Budget Would Buy a Mobile Home. Which One Would You Choose?
## 17 James Carville, the Cajun Who Can’t Stop Ragin’
## 18 A Dark Day Is Coming for Buffalo. It Can’t Wait.
## 19 One Collector’s High Mountain Road to Hokusai
## 20 First He Came for Cancel Culture. Now He Wants to Cancel Smartphones
## section byline published_date
## 1 Books By Elizabeth A. Harris 2024-03-23
## 2 Well By Dana G. Smith 2024-03-20
## 3 Arts By Remy Tumin 2024-03-20
## 4 Opinion By Peter Beinart 2024-03-22
## 5 Real Estate By Tim McKeough 2024-03-22
## 6 Arts By Noel Murray 2020-09-04
## 7 Travel By John Eligon 2024-03-21
## 8 Books By Elisabeth Egan 2024-03-18
## 9 Opinion By Shravani Durbhakula 2024-03-22
## 10 U.S. By Ruth Graham 2024-03-22
## 11 Travel By Valeriya Safronova 2024-03-14
## 12 Arts By Alex Marshall 2024-03-20
## 13 Travel By Ceylan Yeğinsu 2024-03-19
## 14 U.S. By Jeremy W. Peters 2024-03-22
## 15 Well By Anna Maltby 2024-03-06
## 16 Real Estate By Livia Albeck-Ripka 2024-03-21
## 17 Opinion By Maureen Dowd 2024-03-23
## 18 New York By Sarah Maslin Nir 2024-03-23
## 19 Arts By Geraldine Fabrikant 2024-03-18
## 20 Business By Emma Goldberg 2024-03-23
Final view of DataFrame.
kable(MostEmailed)
Analyze data, checked most emailed articles on NYTimes.com from 2020
by section.
MostEmailed %>%
group_by(section,published_date) %>%
filter(published_date > '2020-01-01') %>%
ggplot() +
geom_bar(aes(x = section, fill = published_date)) +
ggtitle("Most Emailed Articles") +
xlab("Sections") +
ylab("Count") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(fill = "Published Year")

Conclussion:
NY Times.com offers a platfiom for developers where we can access
their APIs, after I created an account with them, I was able to obtain a
key access to their website and from there I was able to get the “most
emailed articles API” and import the Json file into R. After that I was
able to create a DataFrame, select the data to work with, analyzed and
visualized the section and published_data column.