# Shout out to Justin Herman for helping me simplify the number of libraries used to one, I had originally planned to use three!

articleSearch <- function (APIkey, APIq="", APIfq="", APIdate_start="", APIdate_end="", APIfl="", APIpage="0"){
  articleSearchURL <- "https://api.nytimes.com/svc/search/v2/articlesearch.json"
  
  APIquery <- list("api-key"=APIkey)
  if (nchar(APIq)>0){
    APIquery <- c(APIquery, "q" = APIq)
  }
  if (nchar(APIfq)>0){
    APIquery <- c(APIquery, "fq" = APIfq)
  }
  if (nchar(APIdate_start)>0){
    APIquery <- c(APIquery, "begin_date" = APIdate_start)
  }
  if (nchar(APIdate_end)>0){
    APIquery <- c(APIquery, "end_date" = APIdate_end)
  }
  if (nchar(APIfl)>0){
    APIquery <- c(APIquery, "fl" = APIfl)
  }
  if (nchar(APIq)==0 & nchar(APIfq)==0 & nchar(APIdate_start)==0 & nchar(APIdate_end)==0 & nchar(APIfl)==0){
    stop("You must enter at least one parameter. You may pick from APIq, APIfq, APIdate_start, APIdate_end, or APIfl.")
  }
  APIquery <- c(APIquery, "facet_filter" = TRUE)
  APIquery <- c(APIquery, "page" = APIpage)
  APIquery <- paste(names(APIquery), APIquery, sep="=")
  APIquery <- paste(APIquery, collapse="&")
  finalURL <- paste(articleSearchURL, APIquery, sep="?")
  finalURL <- URLencode(finalURL)
  rawArticle <- fromJSON(finalURL, flatten = TRUE)
  articles <- as.data.frame(rawArticle$response$docs)
  print(paste("Your query returned", rawArticle$response$meta$hits, "hits.", sep=" "))
  return(articles)
}

This is a function. You assign it to a variable of your own designation. By default, it gives you the first page of results (page 0; results number 1 through 10). It requires you give it an API key. If you have one, it’ll run. It also requires you enter at least one parameter (not including the page number).

Here are some examples:

chicken <- articleSearch(APIkey_usr, "chicken")
## [1] "Your query returned 84967 hits."
names(chicken)
##  [1] "web_url"                 "snippet"                
##  [3] "multimedia"              "keywords"               
##  [5] "document_type"           "section_name"           
##  [7] "type_of_material"        "_id"                    
##  [9] "word_count"              "score"                  
## [11] "source"                  "pub_date"               
## [13] "new_desk"                "uri"                    
## [15] "print_page"              "headline.main"          
## [17] "headline.kicker"         "headline.content_kicker"
## [19] "headline.print_headline" "headline.name"          
## [21] "headline.seo"            "headline.sub"           
## [23] "byline.original"         "byline.person"          
## [25] "byline.organization"
chicken["snippet"]
##                                                                                                                                                                                                                                                       snippet
## 1  <p>If you have seen Robert Kenner's excellent and disturbing movie, "Food Inc.," then you may already have crossed chicken off your list of acceptable foods. Or, like me, you may have resolved to seek out chicken that has been raised in a humane w...
## 2  <p>My niece is about to give birth to her first child, so I decided to fill her freezer with nourishing, comforting chicken stews for those first busy weeks. She loves to cook and to eat well, but she and her husband both know how little time ther...
## 3  Dates add a touch of sweetness to this savory chicken and scallion stir-fry. If you don’t have a wok or a 12-inch skillet, you might want to cook this in two batches in a smaller pan. That will ensure a nice, browned crust on the meat. And if you ...
## 4                                                                                                                                      The company rearranged the letters in its name in an advertisement, and if a vowel was missing, the meaning was clear.
## 5                                                                                                                                                                                         Miso chicken, chicken scaloppine, and more recipes for the weekend.
## 6                                                                                                                                              To give this homey dish a certain elegance, use frozen puff pastry and turn it into fetching, flaky hand pies.
## 7                                                                                                                                                                                                                         Want some fries with that doughnut?
## 8  Chicken salad made by an Iowa food processing company and distributed by Fareway Stores in the Midwest sickened 265 people in eight states and caused one death in Iowa from salmonella contamination, the U.S. Centers for Disease Control and Prevent...
## 9                                                                          A British cooking show has sparked a lively debate across Southeast Asian over whether the widely popular chicken rendang dish should ever be crispy and where it originates from.
## 10                                                       Angry Malaysians on Tuesday defended a Malaysian-born chef who was knocked out of a British cooking competition television show after judges said the chicken dish she served was not crispy enough.
potato <- articleSearch(APIkey_usr, "potato", APIfl="headline")
## [1] "Your query returned 41301 hits."
names(potato)
## [1] "score"                   "headline.main"          
## [3] "headline.kicker"         "headline.content_kicker"
## [5] "headline.print_headline" "headline.name"          
## [7] "headline.seo"            "headline.sub"
potato["headline.main"]
##                                        headline.main
## 1  Potato Plant Poisoning - Green Tubers and Sprouts
## 2          A Satisfying Soup From the Italian Pantry
## 3           Salmon, Two Ways: One Epic, One Everyday
## 4    Weekend Breakfasts to Warm the Heart, and Belly
## 5                        The 400-Degree Thanksgiving
## 6    An Accidentally Creamier, Fluffier Potato Salad
## 7    An Accidentally Creamier, Fluffier Potato Salad
## 8   Matt Orlando Brings California Sun to Copenhagen
## 9                   ‘I Became a Thanksgiving Orphan’
## 10              A Better Beet, Fresh From the Market
# articleSearch(APIkey_usr)

The above is commented out due to the error it causes.

# articleSearch(APIq="potato")

The above is commented out due to the error it causes.