- R client for newsAPIorg
- v1 edition by mkearney, updated to v2 by MD
- Go to newsapi.org and register to get an API key.
- Save the key as an environment variable
## my obscured key
NEWSAPI_KEY <- "4345e85e8ae1427480xxxxxxxxxxxxxx"
## save to .Renviron file
cat(
paste0("NEWSAPI_KEY=", NEWSAPI_KEY),
append = TRUE,
fill = TRUE,
file = file.path("~", ".Renviron")
)
- Install from Github
## install script
if (!"devtools" %in% installed.packages()) {
install.packages("devtools")
}
devtools::install_github("MDshuey/newsAPI")
## load package
library(newsAPI)
- Get all available news sources
## get all english language news sources (made available by newsapi.org)
src <- get_sources(language = "en")
## preview data
print(src, width = 500)
- Pass news source names (IDs) to
get_articles
function
## apply get_articles function to each news source
df <- lapply(src.en$id[-c(42,50,53)], get_articles)
#42, 50, 53 are dud sources, and will return an error.
## collapse into single data frame
df <- do.call("rbind", df)
## preview data
print(df, width = 500)