In this assignment, I will use the httr2 package to send requests through the Web API and gather the most viewed New York Times articles from the past seven days. Additionally, I will convert the results into a data frame.
req <- request("https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key=9qmUA6L7tSCea7ArphemdmGfd0okFsy2")
resp <- req |> req_perform()
resp_json <- resp |> resp_body_json(simplifyVector = TRUE)
df <- resp_json$results
head(df)
## uri
## 1 nyt://article/eb7f1579-7715-5cbc-a0e4-b19d5985fbda
## 2 nyt://article/ab117eb9-c75c-5c63-87f3-9156b96ca30f
## 3 nyt://article/8689c952-a826-53f9-8078-8e9980a0e44d
## 4 nyt://article/a49ceb71-6319-50c6-860f-17084fff4b44
## 5 nyt://article/18a0edf8-2e9e-5d45-826a-bccfbc876824
## 6 nyt://article/072832df-3dc6-555f-ab9d-aa611e8d96ae
## url
## 1 https://www.nytimes.com/2024/10/31/us/united-airlines-passenger-attack.html
## 2 https://www.nytimes.com/2024/10/31/us/politics/jd-vance-joe-rogan.html
## 3 https://www.nytimes.com/2024/11/01/us/politics/russia-ukraine-war.html
## 4 https://www.nytimes.com/2024/11/01/us/politics/liz-cheney-trump-harris.html
## 5 https://www.nytimes.com/2024/11/01/us/politics/trump-immigration-rhetoric-history.html
## 6 https://www.nytimes.com/2024/10/31/style/heidi-klum-halloween-costume-et-alien.html
## id asset_id source published_date updated section
## 1 1e+14 1e+14 New York Times 2024-10-31 2024-11-01 09:47:57 U.S.
## 2 1e+14 1e+14 New York Times 2024-10-31 2024-11-01 22:35:01 U.S.
## 3 1e+14 1e+14 New York Times 2024-11-01 2024-11-01 20:20:33 U.S.
## 4 1e+14 1e+14 New York Times 2024-11-01 2024-11-02 09:44:14 U.S.
## 5 1e+14 1e+14 New York Times 2024-11-01 2024-11-02 09:44:27 U.S.
## 6 1e+14 1e+14 New York Times 2024-10-31 2024-11-01 13:08:56 Style
## subsection nytdsection
## 1 u.s.
## 2 Politics u.s.
## 3 Politics u.s.
## 4 Politics u.s.
## 5 Politics u.s.
## 6 style
## adx_keywords
## 1 Airlines and Airplanes;Assaults;United Airlines
## 2 Abortion;Presidential Election of 2024;Social Media;Podcasts;Transgender;Testosterone;Rogan, Joe;Vance, J D;Trump, Donald J
## 3 United States International Relations;Defense and Military Forces;Russian Invasion of Ukraine (2022);Drones (Pilotless Planes);Foreign Aid;Military Vehicles;Shortages;Draft and Recruitment (Military);Austin, Lloyd J III;Biden, Joseph R Jr;Putin, Vladimir V;Trump, Donald J;Zelensky, Volodymyr;Harris, Kamala D;North Atlantic Treaty Organization;Europe;Moscow (Russia);Kursk (Russia);Donbas (Ukraine);Ukraine;Russia
## 4 Presidential Election of 2024;United States Politics and Government;Threats and Threatening Messages;Harris, Kamala D;Trump, Donald J;Cheney, Liz;Wisconsin
## 5 Presidential Election of 2024;Discrimination;Foreign Workers;Race and Ethnicity;Illegal Immigration;Immigration and Emigration;Hispanic-Americans;Haitian-Americans;Trump, Donald J;Lee, Erika F (Historian);Anbinder, Tyler (1962- );Ngai, Mae;Sandoval-Strausz, A K
## 6 Halloween;Costumes;Parties (Social);Movies;Klum, Heidi;Hard Rock Hotel Inc;New York City
## column byline
## 1 NA By Jesus Jiménez
## 2 NA By Chris Cameron, Simon J. Levien and Neil Vigdor
## 3 NA By Julian E. Barnes, Eric Schmitt, Helene Cooper and Kim Barker
## 4 NA By Reid J. Epstein, Michael Gold and Adam Nagourney
## 5 NA By Jazmine Ulloa
## 6 NA By Madison Malone Kircher
## type title
## 1 Article Man Repeatedly Punched Sleeping Passenger on Flight, Authorities Say
## 2 Article Vance Tells Rogan: Teens Become Trans to Get Into Ivy League
## 3 Article As Russia Advances, U.S. Fears Ukraine Has Entered a Grim Phase
## 4 Article Harris and Trump Rally in Milwaukee as They Make Final Wisconsin Push
## 5 Article The Deep Roots of 4 of Donald Trump’s Nativist Remarks
## 6 Article How Heidi Klum Transformed Into E.T. for Halloween
## abstract
## 1 A man assaulted another passenger on a United Airlines flight on Monday in an attack that appeared to have been random, officials said.
## 2 Appearing on Joe Rogan’s podcast, Senator JD Vance also said that liberal women celebrate their abortions and that Donald Trump would win the “normal gay guy vote.”
## 3 Weapons supplies are no longer Ukraine’s main disadvantage, American military officials say.
## 4 The presidential rivals campaigned in the battleground state’s largest city after Donald Trump tried to slightly soften his violent remarks about Liz Cheney, which Kamala Harris called “disqualifying.”
## 5 In the former president’s pitch to voters, historians hear echoes of the nation’s inescapable xenophobic history.
## 6 The model and television host dressed as the alien from Steven Spielberg’s 1982 film for her annual New York City costume party.
## des_facet
## 1 Airlines and Airplanes, Assaults
## 2 Abortion, Presidential Election of 2024, Social Media, Podcasts, Transgender, Testosterone
## 3 United States International Relations, Defense and Military Forces, Russian Invasion of Ukraine (2022), Drones (Pilotless Planes), Foreign Aid, Military Vehicles, Shortages, Draft and Recruitment (Military)
## 4 Presidential Election of 2024, United States Politics and Government, Threats and Threatening Messages
## 5 Presidential Election of 2024, Discrimination, Foreign Workers, Race and Ethnicity, Illegal Immigration, Immigration and Emigration, Hispanic-Americans, Haitian-Americans
## 6 Halloween, Costumes, Parties (Social), Movies
## org_facet
## 1 United Airlines
## 2
## 3 North Atlantic Treaty Organization
## 4
## 5
## 6 Hard Rock Hotel Inc
## per_facet
## 1
## 2 Rogan, Joe, Vance, J D, Trump, Donald J
## 3 Austin, Lloyd J III, Biden, Joseph R Jr, Putin, Vladimir V, Trump, Donald J, Zelensky, Volodymyr, Harris, Kamala D
## 4 Harris, Kamala D, Trump, Donald J, Cheney, Liz
## 5 Trump, Donald J, Lee, Erika F (Historian), Anbinder, Tyler (1962- ), Ngai, Mae, Sandoval-Strausz, A K
## 6 Klum, Heidi
## geo_facet
## 1
## 2
## 3 Europe, Moscow (Russia), Kursk (Russia), Donbas (Ukraine), Ukraine, Russia
## 4 Wisconsin
## 5
## 6 New York City
## media
## 1 image, photo, A man on a United Airlines flight on Monday repeatedly punched a fellow passenger who was sleeping in what the authorities said appeared to have been a random attack., David J. Phillip/Associated Press, 1, https://static01.nyt.com/images/2024/10/31/multimedia/31xp-flight-fwcl-copy/31xp-flight-fwcl-thumbStandard.jpg, https://static01.nyt.com/images/2024/10/31/multimedia/31xp-flight-fwcl-copy/31xp-flight-fwcl-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/10/31/multimedia/31xp-flight-fwcl-copy/31xp-flight-fwcl-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 2 image, photo, Senator JD Vance interviewed with Joe Rogan., Nick Hagen for The New York Times, 1, https://static01.nyt.com/images/2024/10/31/multimedia/31election-live-vance-rogan-pglc/31election-live-vance-rogan-pglc-thumbStandard.jpg, https://static01.nyt.com/images/2024/10/31/multimedia/31election-live-vance-rogan-pglc/31election-live-vance-rogan-pglc-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/10/31/multimedia/31election-live-vance-rogan-pglc/31election-live-vance-rogan-pglc-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 3 image, photo, Damage from a Russian bombing attack in Sloviansk, Ukraine, last month. Ukraine is losing territory in the east, and its forces inside Russia have been partially pushed back., Tyler Hicks/The New York Times, 1, https://static01.nyt.com/images/2024/11/01/multimedia/01dc-ukraine-military6a-wvlj/01dc-ukraine-military6a-wvlj-thumbStandard.jpg, https://static01.nyt.com/images/2024/11/01/multimedia/01dc-ukraine-military6a-wvlj/01dc-ukraine-military6a-wvlj-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/11/01/multimedia/01dc-ukraine-military6a-wvlj/01dc-ukraine-military6a-wvlj-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 4 image, photo, Vice President Kamala Harris held two rallies in Wisconsin on Friday. , Haiyun Jiang for The New York Times, 1, https://static01.nyt.com/images/2024/12/01/multimedia/01pol-campaign-wzkl/01pol-campaign-wzkl-thumbStandard.jpg, https://static01.nyt.com/images/2024/12/01/multimedia/01pol-campaign-wzkl/01pol-campaign-wzkl-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/12/01/multimedia/01pol-campaign-wzkl/01pol-campaign-wzkl-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 5 image, photo, Former President Donald J. Trump’s nativism echoes sentiments that date back all the way to America’s colonial era., Kenny Holston/The New York Times, 1, https://static01.nyt.com/images/2024/11/01/multimedia/01pol-border-rhetoric-topart-mjtp/01pol-border-rhetoric-topart-mjtp-thumbStandard.jpg, https://static01.nyt.com/images/2024/11/01/multimedia/01pol-border-rhetoric-topart-mjtp/01pol-border-rhetoric-topart-mjtp-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/11/01/multimedia/01pol-border-rhetoric-topart-mjtp/01pol-border-rhetoric-topart-mjtp-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## 6 image, photo, The model Heidi Klum during a fitting on Wednesday for her Halloween costume, inspired by the alien from Steven Spielberg’s 1982 film, “E.T. The Extra-Terrestrial.” , Jutharat Pinyodoonyachet for The New York Times, 1, https://static01.nyt.com/images/2024/10/31/multimedia/HFO-HEIDI-KLUM-COSTUME-01-tfqv/HFO-HEIDI-KLUM-COSTUME-01-tfqv-thumbStandard.jpg, https://static01.nyt.com/images/2024/10/31/multimedia/HFO-HEIDI-KLUM-COSTUME-01-tfqv/HFO-HEIDI-KLUM-COSTUME-01-tfqv-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2024/10/31/multimedia/HFO-HEIDI-KLUM-COSTUME-01-tfqv/HFO-HEIDI-KLUM-COSTUME-01-tfqv-mediumThreeByTwo440.jpg, Standard Thumbnail, mediumThreeByTwo210, mediumThreeByTwo440, 75, 140, 293, 75, 210, 440
## eta_id
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
To clean the data, we can only extract necessary information and modify the format.
mostviewed <- subset(df, select = c(title, byline, abstract, published_date, type, section, subsection))
names(mostviewed)[names(mostviewed) == "byline"] <- "author"
mostviewed$author <- gsub("By", "", mostviewed$author)
mostviewed$published_date <- ymd(mostviewed$published_date)
head(mostviewed)
## title
## 1 Man Repeatedly Punched Sleeping Passenger on Flight, Authorities Say
## 2 Vance Tells Rogan: Teens Become Trans to Get Into Ivy League
## 3 As Russia Advances, U.S. Fears Ukraine Has Entered a Grim Phase
## 4 Harris and Trump Rally in Milwaukee as They Make Final Wisconsin Push
## 5 The Deep Roots of 4 of Donald Trump’s Nativist Remarks
## 6 How Heidi Klum Transformed Into E.T. for Halloween
## author
## 1 Jesus Jiménez
## 2 Chris Cameron, Simon J. Levien and Neil Vigdor
## 3 Julian E. Barnes, Eric Schmitt, Helene Cooper and Kim Barker
## 4 Reid J. Epstein, Michael Gold and Adam Nagourney
## 5 Jazmine Ulloa
## 6 Madison Malone Kircher
## abstract
## 1 A man assaulted another passenger on a United Airlines flight on Monday in an attack that appeared to have been random, officials said.
## 2 Appearing on Joe Rogan’s podcast, Senator JD Vance also said that liberal women celebrate their abortions and that Donald Trump would win the “normal gay guy vote.”
## 3 Weapons supplies are no longer Ukraine’s main disadvantage, American military officials say.
## 4 The presidential rivals campaigned in the battleground state’s largest city after Donald Trump tried to slightly soften his violent remarks about Liz Cheney, which Kamala Harris called “disqualifying.”
## 5 In the former president’s pitch to voters, historians hear echoes of the nation’s inescapable xenophobic history.
## 6 The model and television host dressed as the alien from Steven Spielberg’s 1982 film for her annual New York City costume party.
## published_date type section subsection
## 1 2024-10-31 Article U.S.
## 2 2024-10-31 Article U.S. Politics
## 3 2024-11-01 Article U.S. Politics
## 4 2024-11-01 Article U.S. Politics
## 5 2024-11-01 Article U.S. Politics
## 6 2024-10-31 Article Style
section <- mostviewed %>% count(section)
print(section)
## section n
## 1 Arts 1
## 2 Briefing 1
## 3 Business 1
## 4 New York 2
## 5 Opinion 3
## 6 Style 1
## 7 The Upshot 1
## 8 U.S. 9
## 9 Well 1
ggplot(section, aes(x = section, y = n, fill = section)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Number of Articles in Each Section", y = "number of articles")
subsection <- mostviewed %>% group_by(section) %>%
filter(section == "U.S.") %>% count(subsection)
subsection$subsection[subsection$subsection == ""] <- "No Subsection"
print(subsection)
## # A tibble: 3 × 3
## # Groups: section [1]
## section subsection n
## <chr> <chr> <int>
## 1 U.S. No Subsection 1
## 2 U.S. 2024 Elections 1
## 3 U.S. Politics 7
ggplot(subsection, aes(x = subsection, y = n, fill = subsection)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Number of Articles in U.S.'s Subsection", y = "number of articles")
The data obtained from the API indicates that the most frequently accessed articles pertain to U.S. politics. This trend suggests that there is a significant level of interest among readers in political matters.
Overall, sending requests through the WebApi was straightforward and can be easily accomplished using the httr2 package. Additionally, the information obtained can be used for analysis. In this case, it’s evident that U.S. politics is a popular topic that can attract a significant number of views. One way to enhance this assignment would be to explore other packages to determine which option is easier and more effective.