Skip to content

Instantly share code, notes, and snippets.

@vanatteveldt
Last active November 22, 2019 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanatteveldt/45b2a892d53b6a88f16cb9febfc51790 to your computer and use it in GitHub Desktop.
Save vanatteveldt/45b2a892d53b6a88f16cb9febfc51790 to your computer and use it in GitHub Desktop.
# 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