Skip to content

Instantly share code, notes, and snippets.

@vanatteveldt

vanatteveldt/nytimes.r

Last active Nov 22, 2019
Embed
What would you like to do?
# I will try to post a longer handout, but here is the needed code for accessing nytimes.
# See also: https://github.com/mkearney/nytimes
# 1. Install the package from github:
install.packages("devtools")
devtools::install_github("mkearney/nytimes")
# 2. Get an API key from nytimes
# - create an account at https://developer.nytimes.com/accounts/create
# - activate the account from your email and log in
# - create a new 'app' (in your username menu) and select article search API
# - record the API key and enter it below:
Sys.setenv("NYTIMES_KEY"="...")
# 3. Search!
library(nytimes)
nytsearch <- data.frame.search(nyt_search("sanctions", n = 20))
# To convert to a quanteda dfm:
library(quanteda)
corp = corpus(nytsearch, docid_field = "id", text_field="lead_paragraph")
dfm = dfm(corp, remove=stopwords("english"), remove_punct=T)
textplot_wordcloud(dfm, max_words=100)
# If you want to convert to tidyverse, first convert date to POSIXct
library(tidyverse)
nytsearch$pub_date = as.POSIXct(nytsearch$pub_date)
nytsearch = as_tibble(nytsearch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.