library(tidyverse)
library(kableExtra)
library(httr)
library(jsonlite)
api.key <- params$api_key

Create an R wrapper funciton for NYTimes Article Search API

The function will search Travel section and news desk for articles matching given query term word(s), such as a country name, for example. It is designed to return a data frame with 5 columns:
  1. headline
  2. publication date
  3. web url
  4. snippet
nytimes.articleSearch.on.travel <- function(api_key = NA, qryTermTravelPlace = "New York", begin_date = "yyyymmdd", end_date = "yyyymmdd") {
  baseUrl <- "https://api.nytimes.com/svc/search/v2/articlesearch.json"
  
  baseUrlParam <- URLencode(
    sprintf("?fq=section_name:(\"Travel\") OR news_desk:(\"Travel\")&fl=%s&api-key=%s",
            "web_url,snippet,headline,pub_date,print_page", api_key))
  
  qryTermParam <- URLencode(sprintf("&q=%s", qryTermTravelPlace))
  
  qryBeginDate <- ""
  if (begin_date != "yyyymmdd" && !is.na(begin_date)) {
    qryBeginDate <- sprintf("&begin_date=%s", begin_date)
  }
  
  qryEndDate <- ""
  if (end_date != "yyyymmdd" && !is.na(end_date)) {
    qryEndDate <- sprintf("&end_date=%s", end_date)
  }
  
  qryResult <- GET(paste0(baseUrl, baseUrlParam, qryTermParam, qryBeginDate, qryEndDate))

  df.content <- fromJSON(content(qryResult, "text"))
  
  df.on.Travel <- data.frame(
    headline = df.content[["response"]][["docs"]][["headline"]][["main"]],
    pub_date = df.content[["response"]][["docs"]][["pub_date"]],
    web_url = df.content[["response"]][["docs"]][["web_url"]],
    snippet = df.content[["response"]][["docs"]][["snippet"]]
  )
  
  return(df.on.Travel)
}

Test the function with different parameters and values

df.nytimes.Travel <- nytimes.articleSearch.on.travel(api.key, "Costa Rica") %>% arrange(desc(pub_date))
kable_styling(knitr::kable(df.nytimes.Travel, "html", caption = "Travel Search Results for Costa Rica"), bootstrap_options = "striped")
Travel Search Results for Costa Rica
headline pub_date web_url snippet
On the Costa Rican Coast, Finding Fun by Escaping Exclusivity 2018-03-27T09:00:08+0000 https://www.nytimes.com/2018/03/27/travel/peninsula-papagayo-costa-rica-52-places.html Peninsula Papagayo is where the ultrarich go to avoid having to interact with the regular rich. But our 52 Places columnist finds a way to get away from the traps of luxury.
Celebrating International Women’s Day With Free Hotel Amenities 2018-02-27T10:00:43+0000 https://www.nytimes.com/2018/02/27/travel/international-womens-day-hotel-deals.html A range of activities that allow female travelers to connect are being offered free of charge at hotels in celebration of International Women’s Day.
After a Tragedy, Making the Case for Costa Rica 2018-01-05T20:02:42+0000 https://www.nytimes.com/2018/01/05/travel/costa-rica-crash-ecotravel.html The tragedy of the crash is particularly difficult to reconcile with the remarkable beauty of Costa Rica — a place I wouldn’t hesitate to return to.
How to Check the Safety of a Charter Airline 2018-01-05T19:00:16+0000 https://www.nytimes.com/2018/01/05/travel/charter-airline-safety-tips.html The fatal crash of a flight in Costa Rica has prompted questions about how to assess the reliability of charter services. Here’s how to investigate.
After a Plane Crash, Questions About Travel in Costa Rica 2018-01-04T18:50:09+0000 https://www.nytimes.com/2018/01/04/travel/costa-rica-air-travel-safety.html The country is a popular ecotourism destination for Americans — but the fatal crash of a charter flight has prompted inquiries about transportation alternatives.
Leisure Travel With an Eye on Giving Back 2017-12-08T10:00:31+0000 https://www.nytimes.com/2017/12/08/travel/vounteer-vacation-project-voluntourism.html There are increasing opportunities to combine travel and volunteering — and one company is leading the way for budget-friendly options.
In the Rainy Season, Savings in Asia and Central America 2017-09-06T13:59:10+0000 https://www.nytimes.com/2017/09/06/travel/rainy-season-savings.html Resorts in Cambodia, Thailand, Costa Rica and Guatemala are offering packages and lower prices in what is sometimes called the green season.
On the Costa Rican Coast, Finding Pura Vida on a Budget 2017-05-31T09:00:35+0000 https://www.nytimes.com/2017/05/31/travel/costa-rica-eco-travel-budget-drake-bay.html In Drake Bay, it’s possible to truly disconnect. Better yet, I was able to relish this enjoyable tropical getaway for a very reasonable sum.
Five Ways to be a Savvy Medical Tourist and Enjoy a Vacation 2017-03-08T10:00:21+0000 https://www.nytimes.com/2017/03/08/travel/five-ways-to-be-a-medical-dental-tourist-vacation.html When costly dental work became unavoidable, the Frugal Family decided to get it done abroad and let the savings pay for a tropical week in Thailand.
In Costa Rica, Photographing Jaguars to Help Save Them 2016-08-11T10:00:04+0000 https://www.nytimes.com/2016/08/14/travel/costa-rica-eco-tourism.html Conservationists and eco-lodges in Costa Rica are using camera “traps” to record jaguars and assess the environment.
df.nytimes.Travel <- nytimes.articleSearch.on.travel(api.key, "Israel", begin_date = "20170601", end_date = "20170831") %>% arrange(desc(pub_date))
kable_styling(knitr::kable(df.nytimes.Travel, "html", caption = "Travel Search Results for Israel, between June and August, 2017"), bootstrap_options = "striped")
Travel Search Results for Israel, between June and August, 2017
headline pub_date web_url snippet
In Israel, Affordable Desert Adventures 2017-08-26T10:00:26+0000 https://www.nytimes.com/2017/08/26/travel/affordable-israel-desert-adventures.html The Negev Desert offers craters, cliffs, oases, Arabian leopards — and inexpensive lodging.
In Kazakhstan, a World Expo Is All About Energy (and Dancing) 2017-08-23T09:00:37+0000 https://www.nytimes.com/2017/08/23/travel/kazakhstan-world-expo-astana.html The “Future Energy” pavilions at Expo 2017 in Astana celebrate solar, wind and thermal power. There also are live bands — and Nathan’s hot dogs.
A Jerusalem Restaurant With Italian and Israeli Influences 2017-07-28T09:00:19+0000 https://www.nytimes.com/2017/07/28/travel/anna-restaurant-jerusalem-israel-review.html The best dishes at Anna, which opened in 2016, encompass the shared Italian and Israeli passion for freshness and simplicity.
A London Kitchen Turning Out Israeli Street Food 2017-06-02T04:01:24+0000 https://www.nytimes.com/2017/06/02/travel/bala-baya-restaurant-london-israel-review.html Bala Baya, the latest and hippest of the English capital’s Israeli restaurants, serves extravagantly flavored dishes that transcend their humble roots.

NYTimes Attribution Requirement
The logo must link directly to http://developer.nytimes.com