Task

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 Data Frame.

Code

Libraries:

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.4     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(httr)
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
## 
##     flatten
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows

I plan to extract data from the Top Stories API which will retrieve the Top Stories from the New York Times Newspaper. I plan to extract the top stories that relate to the United States and top stories related to non-US news.

# api key
api.key <- '2UA3cNoRNwal6giF6kSPsQFfQr1dppUa'

# Get data from API: US and World

# url for United States data
top.stories.us.url <- paste("https://api.nytimes.com/svc/topstories/v2/us.json?api-key=", api.key, sep='')

# convert json data received from api to a data frame
us.top.stories.data <- fromJSON(top.stories.us.url) %>% 
  as.data.frame()


# url for world data
top.stories.world.url <- paste("https://api.nytimes.com/svc/topstories/v2/world.json?api-key=", api.key, sep='')

# convert json data received from api to a data frame
world.top.stories.data <- fromJSON(top.stories.world.url) %>% 
  as.data.frame()

top.stories.us.and.world<- rbind(us.top.stories.data,world.top.stories.data)
kbl(top.stories.us.and.world[1:5,]) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
status copyright section last_updated num_results results.section results.subsection results.title results.abstract results.url results.uri results.byline results.item_type results.updated_date results.created_date results.published_date results.material_type_facet results.kicker results.des_facet results.org_facet results.per_facet results.geo_facet results.multimedia results.short_url
OK Copyright (c) 2021 The New York Times Company. All Rights Reserved. U.S. News 2021-10-23T09:04:11-04:00 28 us Energizing Conservative Voters, One School Board Election at a Time Republicans hope that concerns about critical race theory can help them in the midterm elections. The issue has torn apart one Wisconsin suburb. https://www.nytimes.com/2021/10/21/us/republicans-schools-critical-race-theory.html nyt://article/20d7d1ba-d3d7-52b8-9421-c1bfa62ca38e By Stephanie Saul Article 2021-10-22T16:48:54-04:00 2021-10-21T11:56:37-04:00 2021-10-21T11:56:37-04:00 Education (K-12) , Critical Race Theory , Teachers and School Employees , Race and Ethnicity , Black People , Midterm Elections (2022) , Conservatism (US Politics) , Politics and Government , United States Politics and Government Republican Party Wisconsin , Mequon (Wis) https://static01.nyt.com/images/2021/10/11/us/00school-boards-1/00school-boards-1-superJumbo.jpg , https://static01.nyt.com/images/2021/10/11/us/00school-boards-1/00school-boards-1-thumbStandard.jpg , https://static01.nyt.com/images/2021/10/11/us/00school-boards-1/00school-boards-1-thumbLarge.jpg , https://static01.nyt.com/images/2021/10/11/us/00school-boards-1/00school-boards-1-mediumThreeByTwo210.jpg , https://static01.nyt.com/images/2021/10/11/us/00school-boards-1/00school-boards-1-articleInline.jpg , superJumbo , Standard Thumbnail , thumbLarge , mediumThreeByTwo210 , Normal , 1364 , 75 , 150 , 140 , 127 , 2048 , 75 , 150 , 210 , 190 , image , image , image , image , image , photo , photo , photo , photo , photo , Volunteers collecting signatures in August to recall members of the Mequon-Thiensville School District board, in Wisconsin., Volunteers collecting signatures in August to recall members of the Mequon-Thiensville School District board, in Wisconsin., Volunteers collecting signatures in August to recall members of the Mequon-Thiensville School District board, in Wisconsin., Volunteers collecting signatures in August to recall members of the Mequon-Thiensville School District board, in Wisconsin., Volunteers collecting signatures in August to recall members of the Mequon-Thiensville School District board, in Wisconsin., Morry Gash/Associated Press , Morry Gash/Associated Press , Morry Gash/Associated Press , Morry Gash/Associated Press , Morry Gash/Associated Press https://nyti.ms/3m23V0D
OK Copyright (c) 2021 The New York Times Company. All Rights Reserved. U.S. News 2021-10-23T09:04:11-04:00 28 us In Mourning and Gratitude, the Blackfeet Nation Says Farewell to Its Leader Over four days in Montana, members honored Chief Earl Old Person, who led the tribe and preserved its history for more than 60 years. https://www.nytimes.com/2021/10/23/us/chief-earl-old-person-blackfeet-nation.html nyt://article/1ab0b85a-c38d-5f62-8ba0-4563a4702aaf By Tailyr Irvine Article 2021-10-24T07:41:26-04:00 2021-10-23T05:00:19-04:00 2021-10-23T05:00:19-04:00 Funerals and Memorials, Native Americans Old Person, Earl (1929-2021) Blackfeet Indian Reservation (Mont), Montana https://static01.nyt.com/images/2021/10/23/us/23chief-earl-5/23chief-earl-5-superJumbo.jpg , https://static01.nyt.com/images/2021/10/23/us/23chief-earl-5/23chief-earl-5-thumbStandard.jpg , https://static01.nyt.com/images/2021/10/23/us/23chief-earl-5/23chief-earl-5-thumbLarge.jpg , https://static01.nyt.com/images/2021/10/23/us/23chief-earl-5/23chief-earl-5-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2021/10/23/us/23chief-earl-5/23chief-earl-5-articleInline.jpg , superJumbo , Standard Thumbnail , thumbLarge , mediumThreeByTwo210 , Normal , 1365 , 75 , 150 , 140 , 127 , 2048 , 75 , 150 , 210 , 190 , image , image , image , image , image , photo , photo , photo , photo , photo , , , , , , , , , , https://nyti.ms/3nnj3F3
OK Copyright (c) 2021 The New York Times Company. All Rights Reserved. U.S. News 2021-10-23T09:04:11-04:00 28 us Minneapolis Police Officer Receives Shorter Sentence in Fatal Shooting Mohamed Noor shot a resident who had called 911 seeking help. His conviction was seen as a rare example of an officer facing serious consequences for an unjustified shooting. https://www.nytimes.com/2021/10/21/us/mohamed-noor-minneapolis-sentenced.html nyt://article/778808d5-2ea4-5303-bd54-25dab029d816 By Mitch Smith Article 2021-10-21T23:05:00-04:00 2021-10-21T11:39:15-04:00 2021-10-21T11:39:15-04:00 Murders, Attempted Murders and Homicides Police Department (Minneapolis, Minn) Damond, Justine (d 2017) , Noor, Mohamed (Minneapolis, Minn, Police Officer) Minneapolis (Minn) https://static01.nyt.com/images/2021/10/21/us/21noor-1/merlin_196544823_8adc7ceb-2def-416b-8238-c3c735071bba-superJumbo.jpg , https://static01.nyt.com/images/2021/10/21/us/21noor-1/merlin_196544823_8adc7ceb-2def-416b-8238-c3c735071bba-thumbStandard.jpg , https://static01.nyt.com/images/2021/10/21/us/21noor-1/merlin_196544823_8adc7ceb-2def-416b-8238-c3c735071bba-thumbLarge.jpg , https://static01.nyt.com/images/2021/10/21/us/21noor-1/merlin_196544823_8adc7ceb-2def-416b-8238-c3c735071bba-mediumThreeByTwo210.jpg , https://static01.nyt.com/images/2021/10/21/us/21noor-1/merlin_196544823_8adc7ceb-2def-416b-8238-c3c735071bba-articleInline.jpg , superJumbo , Standard Thumbnail , thumbLarge , mediumThreeByTwo210 , Normal , 1368 , 75 , 150 , 140 , 127 , 2048 , 75 , 150 , 210 , 190 , image , image , image , image , image , photo , photo , photo , photo , photo , Mohamed Noor, a former Minneapolis police officer, was originally sentenced at Hennepin County District Court in Minneapolis in 2019., Mohamed Noor, a former Minneapolis police officer, was originally sentenced at Hennepin County District Court in Minneapolis in 2019., Mohamed Noor, a former Minneapolis police officer, was originally sentenced at Hennepin County District Court in Minneapolis in 2019., Mohamed Noor, a former Minneapolis police officer, was originally sentenced at Hennepin County District Court in Minneapolis in 2019., Mohamed Noor, a former Minneapolis police officer, was originally sentenced at Hennepin County District Court in Minneapolis in 2019., Pool photo by Leila Navidi , Pool photo by Leila Navidi , Pool photo by Leila Navidi , Pool photo by Leila Navidi , Pool photo by Leila Navidi https://nyti.ms/3jqk5iF
OK Copyright (c) 2021 The New York Times Company. All Rights Reserved. U.S. News 2021-10-23T09:04:11-04:00 28 us Alabama’s Next Poet Laureate Writes Searingly About Race Ashley M. Jones, whose latest collection is called “Reparations Now!,” is unflinching in her criticism of those in power and the myriad inequalities in American life. https://www.nytimes.com/2021/10/21/us/ashley-jones-poet-laureate-alabama.html nyt://article/fff59927-f132-53fc-a26b-cc770140b927 By Tariro Mzezewa Article 2021-10-22T08:06:42-04:00 2021-10-21T05:00:22-04:00 2021-10-21T05:00:22-04:00 Poets Laureate , Poetry and Poets , Content Type: Personal Profile, Black People , Discrimination , Reparations , Reparations Now! (Book) , Books and Literature Jones, Ashley M (Poet) Alabama https://static01.nyt.com/images/2021/10/18/us/00alabama-poet/00alabama-poet-superJumbo.jpg , https://static01.nyt.com/images/2021/10/18/us/00alabama-poet/00alabama-poet-thumbStandard.jpg , https://static01.nyt.com/images/2021/10/18/us/00alabama-poet/00alabama-poet-thumbLarge.jpg , https://static01.nyt.com/images/2021/10/18/us/00alabama-poet/00alabama-poet-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2021/10/18/us/00alabama-poet/00alabama-poet-articleInline.jpg , superJumbo , Standard Thumbnail , thumbLarge , mediumThreeByTwo210 , Normal , 2048 , 75 , 150 , 140 , 238 , 1639 , 75 , 150 , 210 , 190 , image , image , image , image , image , photo , photo , photo , photo , photo , , , , , , Kenny Holston for The New York Times , Kenny Holston for The New York Times , Kenny Holston for The New York Times , Kenny Holston for The New York Times , Kenny Holston for The New York Times https://nyti.ms/3GhD5K2
OK Copyright (c) 2021 The New York Times Company. All Rights Reserved. U.S. News 2021-10-23T09:04:11-04:00 28 us Questions on Baldwin Shooting Turn to Assistant Director Complaints have emerged about his oversight of safety measures on previous productions. https://www.nytimes.com/2021/10/24/us/baldwin-shooting-assistant-director.html nyt://article/b9c29da7-bd21-5ac3-b1fe-d14f22f08113 By Simon Romero, Julia Jacobs and Graham Bowley Article 2021-10-24T21:10:30-04:00 2021-10-24T21:10:30-04:00 2021-10-24T21:10:30-04:00 Movies , Firearms , Actors and Actresses, Rust (Movie) Blumhouse Productions , Directors Guild of America Baldwin, Alec , Bogdanovich, Antonia (1967- ), Hutchins, Halyna (1979-2021) Albuquerque (NM), Santa Fe (NM) https://static01.nyt.com/images/2021/10/24/us/24baldwin-prop-1/merlin_196684830_520b6f28-8b87-48e4-b080-acf221ea3141-superJumbo.jpg , https://static01.nyt.com/images/2021/10/24/us/24baldwin-prop-1/24baldwin-prop-1-thumbStandard.jpg , https://static01.nyt.com/images/2021/10/24/us/24baldwin-prop-1/24baldwin-prop-1-thumbLarge.jpg , https://static01.nyt.com/images/2021/10/24/us/24baldwin-prop-1/merlin_196684830_520b6f28-8b87-48e4-b080-acf221ea3141-mediumThreeByTwo210.jpg, https://static01.nyt.com/images/2021/10/24/us/24baldwin-prop-1/merlin_196684830_520b6f28-8b87-48e4-b080-acf221ea3141-articleInline.jpg , superJumbo , Standard Thumbnail , thumbLarge , mediumThreeByTwo210 , Normal , 1365 , 75 , 150 , 140 , 127 , 2048 , 75 , 150 , 210 , 190 , image , image , image , image , image , photo , photo , photo , photo , photo , The entrance to the Bonanza Creek Film Ranch, where the actor Alec Baldwin fired a prop gun, killing the cinematographer Halyna Hutchins. , The entrance to the Bonanza Creek Film Ranch, where the actor Alec Baldwin fired a prop gun, killing the cinematographer Halyna Hutchins. , The entrance to the Bonanza Creek Film Ranch, where the actor Alec Baldwin fired a prop gun, killing the cinematographer Halyna Hutchins. , The entrance to the Bonanza Creek Film Ranch, where the actor Alec Baldwin fired a prop gun, killing the cinematographer Halyna Hutchins. , The entrance to the Bonanza Creek Film Ranch, where the actor Alec Baldwin fired a prop gun, killing the cinematographer Halyna Hutchins. , Jae C. Hong/Associated Press , Jae C. Hong/Associated Press , Jae C. Hong/Associated Press , Jae C. Hong/Associated Press , Jae C. Hong/Associated Press https://nyti.ms/3vJP66a

Conclusion

The New York Times API make querying data fast and easy. As you can see above, it only requires a few lines of code to transform the Top Stories API into a data frame.