For this week’s assignment, I had to choose one of the New York Times APIs and transform it into a R data frame. I decided to go with the API that provides the most popular/viewed articles for the last seven days.

Importing the libraries

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

Getting the url and keys

nyt_url <- "https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key="

api_key <- "mzhmTbbWUMXoIJzATs62x7LzpyAQ5EA9"

full_url <- base::paste0(nyt_url,api_key)
full_url
## [1] "https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key=mzhmTbbWUMXoIJzATs62x7LzpyAQ5EA9"

Calling the API and checking the response

api_call <- httr::GET(full_url)

api_call$status_code
## [1] 200

Converting the response to a data frame

api_char <- rawToChar(api_call$content)

api_JSON <- fromJSON(api_char, flatten = TRUE)

df <- data.frame(api_JSON)
glimpse(df)
## Rows: 20
## Columns: 25
## $ status                 <chr> "OK", "OK", "OK", "OK", "OK", "OK", "OK", "OK",…
## $ copyright              <chr> "Copyright (c) 2023 The New York Times Company.…
## $ num_results            <int> 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,…
## $ results.uri            <chr> "nyt://article/5c326d97-c547-50e9-8ad0-83ee9305…
## $ results.url            <chr> "https://www.nytimes.com/2023/03/25/world/europ…
## $ results.id             <dbl> 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14…
## $ results.asset_id       <dbl> 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14, 1e+14…
## $ results.source         <chr> "New York Times", "New York Times", "New York T…
## $ results.published_date <chr> "2023-03-25", "2023-03-24", "2023-03-22", "2023…
## $ results.updated        <chr> "2023-03-26 08:25:41", "2023-03-26 16:29:58", "…
## $ results.section        <chr> "World", "U.S.", "Health", "U.S.", "Style", "Op…
## $ results.subsection     <chr> "Europe", "", "", "", "", "", "Politics", "", "…
## $ results.nytdsection    <chr> "world", "u.s.", "health", "u.s.", "style", "op…
## $ results.adx_keywords   <chr> "Russian Invasion of Ukraine (2022);Defense and…
## $ results.column         <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ results.byline         <chr> "By Justin Scheck and Thomas Gibbons-Neff", "By…
## $ results.type           <chr> "Article", "Article", "Article", "Article", "Ar…
## $ results.title          <chr> "Stolen Valor: The U.S. Volunteers in Ukraine W…
## $ results.abstract       <chr> "People who would not be allowed anywhere near …
## $ results.des_facet      <list> <"Russian Invasion of Ukraine (2022)", "Defens…
## $ results.org_facet      <list> <>, "RM Palmer Co", "Current Biology (Journal)…
## $ results.per_facet      <list> <>, <>, "Beethoven, Ludwig van", <>, <>, <>, <…
## $ results.geo_facet      <list> <"Ukraine", "Russia", "United States">, "West …
## $ results.media          <list> [<data.frame[1 x 6]>], [<data.frame[0 x 0]>], …
## $ results.eta_id         <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…

Creating a data frame of the columns I need

popular_articles <- df %>% 
  select(results.published_date,results.title, results.section)

colnames(popular_articles) <- c("Published Date","Popular Articles","Section")  

knitr:: kable(popular_articles)
Published Date Popular Articles Section
2023-03-25 Stolen Valor: The U.S. Volunteers in Ukraine Who Lie, Waste and Bicker World
2023-03-24 Death Toll in Chocolate Factory Explosion Rises to Five U.S.
2023-03-22 DNA From Beethoven’s Hair Unlocks Medical and Family Secrets Health
2023-03-25 At Least 26 Killed as Powerful Tornado Tears Through Mississippi U.S.
2023-03-25 Think Getting Into College Is Hard? Try Applying for These Gyms. Style
2023-03-25 I Am Haunted by What I Have Seen at Great Salt Lake Opinion
2023-03-25 As Trump Rallies in Waco, His Followers Shore Up His 2024 Bid U.S.
2023-03-25 Whatever the Problem, It’s Probably Solved by Walking Opinion
2023-03-24 Gordon E. Moore, Intel Co-Founder Behind Moore’s Law, Dies at 94 Technology
2023-03-24 Say Goodbye to Daily Hotel Room Cleaning Travel
2023-03-24 The Man Who Leaked the Pentagon Papers Is Scared Opinion
2023-03-24 These Devices Sickened Hundreds. The New Models Have Risks, Too. Health
2023-03-23 For Sale: Mansions in Los Angeles at Bargain Prices Real Estate
2023-03-24 Conflict in Syria Escalates Following Attack That Killed a U.S. Contractor U.S.
2020-09-10 The Best Movies on Amazon Prime Video Right Now Arts
2023-03-24 You Can Have the Blue Pill or the Red Pill, and We’re Out of Blue Pills Opinion
2023-03-24 The Northern Lights Were Seen Farther South in the United States U.S.
2023-03-24 What We Learned in March Madness on Friday Sports
2023-03-24 Should You Get Another Covid Booster? Health
2020-09-10 The 50 Best Movies on Netflix Right Now Movies