The goal of nytapi is to get data from the New York Times API.
You can install the development version of nytapi from GitHub with:
# install.packages("devtools")
devtools::install_github("clessn/nytapi")
- Get API key from developer.nytimes.com/
- Save the access token to R environ using
usethis::edit_r_environ()
# .Renviron
NYT_KEY = "keystring"
- Use key in functions
search_articles(query = "Wikipedia", key = Sys.getenv("NYT_KEY"))
library("nytapi")
json <- search_articles(query = "Wikipedia", key = Sys.getenv("NYT_KEY"))
# Preview of first article
article <- json[["response"]][["docs"]][[1]]
article[["abstract"]]
#> [1] "Women are being removed from the list of “American Novelists” and put into their own category. Moves like that make it harder and slower for women to gain equality in the literary world."
article[["web_url"]]
#> [1] "https://www.nytimes.com/2013/04/28/opinion/sunday/wikipedias-sexism.html"
article[["snippet"]]
#> [1] "Women are being removed from the list of “American Novelists” and put into their own category. Moves like that make it harder and slower for women to gain equality in the literary world."
json <- get_most_viewed(key = Sys.getenv("NYT_KEY"))
# Preview of first article
article <- json[["results"]][[1]]
article[["title"]]
#> [1] "Gunman Kills 5 Co-Workers at Louisville Bank on Livestream, Police Say"
article[["byline"]]
#> [1] "By Kevin Williams, Amanda Holpuch and Campbell Robertson"
article[["published_date"]]
#> [1] "2023-04-10"
json <- get_most_emailed(key = Sys.getenv("NYT_KEY"))
# Preview of first article
article <- json[["results"]][[1]]
article[["title"]]
#> [1] "Whatever the Problem, It’s Probably Solved by Walking"
article[["byline"]]
#> [1] "By Andrew McCarthy"
article[["published_date"]]
#> [1] "2023-03-25"
For API documentation, terms of service and licensing information, see developer.nytimes.com.
nytapi is not afiliated with The New York Times.