Fetching data from New York Times APIs

The New York Times web site provides a rich set of APIs, as described here.

  1. Sign up for at least one API key
  2. Choose an API to search and construct an interface in R to read in the JSON data
  3. Transform the data into an R dataframe
library(jsonlite)
#search for articles with keyword "halloween"

article_key <- "&api-key=e521540e572de04780c19a7788fb0836:5:70863196" # my API key
url <- "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=halloween"
req <- fromJSON(paste0(url, article_key))
h_articles <- req$response$docs
colnames(h_articles) # print the column names
##  [1] "web_url"           "snippet"           "lead_paragraph"   
##  [4] "abstract"          "print_page"        "blog"             
##  [7] "source"            "multimedia"        "headline"         
## [10] "keywords"          "pub_date"          "document_type"    
## [13] "news_desk"         "section_name"      "subsection_name"  
## [16] "byline"            "type_of_material"  "_id"              
## [19] "word_count"        "slideshow_credits"
class(h_articles) # make sure it's a dataframe
## [1] "data.frame"
h_articles[1:10,9] # Show the first 10 headlines from the query 
##                                                                main
## 1                                     How the 1 Percent Says ‘Boo!’
## 2      Halloween Thrills and Festivities, With or Without Costumes 
## 3                                              Friday Night Frights
## 4                          This Halloween, Be the Talk of the Party
## 5  Walmart Withdraws Hooked ‘Sheik Fagin’ Nose From Halloween Store
## 6                                                Halloween Costumes
## 7                             The Death and Rebirth of Bart Simpson
## 8                 A Werewolfathon at Landmark Loew’s Jersey Theater
## 9                Halloween Makeup Ideas That Are Scary Good-Looking
## 10                   First, Kill the Witches. Then, Celebrate Them.
##                                                      print_headline
## 1                                     How the 1 Percent Says ‘Boo!’
## 2                                                              <NA>
## 3                                                              <NA>
## 4                         This Halloween,  Be the Talk of the Party
## 5  Walmart Withdraws Hooked ‘Sheik Fagin’ Nose From Halloween Store
## 6                                                              <NA>
## 7                              Television; Bart Simpson, Born Again
## 8                                    Film; Silver Bullets Must Work
## 9                                                Makeup as Dress-Up
## 10                   First, Kill the Witches. Then, Celebrate Them.
##       content_kicker            kicker
## 1               <NA>              <NA>
## 2      Weekend Miser              <NA>
## 3               <NA>              <NA>
## 4  Op-Ed Contributor Op-Ed Contributor
## 5        Open Source       Open Source
## 6               <NA>          Wordplay
## 7               <NA>              <NA>
## 8               <NA>              <NA>
## 9          Skin Deep         Skin Deep
## 10           Opinion           Opinion

More documentation on using R to fetch JSON data from REST APIs is available here