First at all, we install and load the two required libraries: RJSONIO, RCurl, and plotly.

if (!require("RCurl")) install.packages("RCurl", repos="https://cran.cnr.berkeley.edu/", dependencies = TRUE)
if (!require("RJSONIO")) install.packages("RJSONIO", repos="https://cran.cnr.berkeley.edu/", dependencies = TRUE)
if (!require("plotly")) install.packages("plotly", repos="https://cran.cnr.berkeley.edu/", dependencies = TRUE)

library(RJSONIO)
library(RCurl)
library(plotly)

Please register and obtain your own API key here http://developer.nytimes.com/ and put it into the following code.

#### API key here
api <- "YOUR KEY HERE" 

Let’s do a trial run to search “South Korea” in the New York Times 2018 archive. variable tesing is a list.

year <- 2018
search_q <- URLencode("'South Korea'")
url <- paste('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=',search_q,'&begin_date=',year,'0101&end_date=',year,'1231&api-key=',api,sep="")
testing <- fromJSON(getURL(url))  ### Convert the API output to R Object
class(testing)
## [1] "list"
str(testing$response$meta)  # Show its data structure
##  Named num [1:3] 2026 0 21
##  - attr(*, "names")= chr [1:3] "hits" "offset" "time"
print(testing$response$meta["hits"]) ### Display the number of hits in 2018
## hits 
## 2026
Sys.sleep(5) ### Wait a while to limit within 1 call per second

Next, define your variables: search term search_q, time duration year_range, and a empty variable nyt_china for storing the results.

search_q <- URLencode("'China'")
year_range <- 1990:2018 
nyt_china <- data.frame(year=character(0),hits=numeric(0))

Getting each year’s article count via a for loop

for(year in year_range){
    url <- paste('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=',search_q,'&begin_date=',year,'0101&end_date=',year,'1231&api-key=',api,sep="")
    nyt_robj <- fromJSON(getURL(url))  ## Convert from JSON to R Object
  hits <- nyt_robj$response$meta["hits"]   ## save the article hits
    nyt_china <- rbind(nyt_china,data.frame(year=year,hits=hits))   ## Add a new row to nyt_china
    print(paste(year,hits,sep=":"))  ### Print the new row contents
    Sys.sleep(5) ### Wait a while to limit within 1 call per second
}
## [1] "1990:3124"
## [1] "1991:2801"
## [1] "1992:2895"
## [1] "1993:2920"
## [1] "1994:3102"
## [1] "1995:3246"
## [1] "1996:3512"
## [1] "1997:3841"
## [1] "1998:4099"
## [1] "1999:4310"
## [1] "2000:4413"
## [1] "2001:4198"
## [1] "2002:3667"
## [1] "2003:4226"
## [1] "2004:4034"
## [1] "2005:4497"
## [1] "2006:5949"
## [1] "2007:7008"
## [1] "2008:8209"
## [1] "2009:8188"
## [1] "2010:9481"
## [1] "2011:9261"
## [1] "2012:9696"
## [1] "2013:7479"
## [1] "2014:8301"
## [1] "2015:7450"
## [1] "2016:6057"
## [1] "2017:5795"
## [1] "2018:6654"

We then repeat the search for “Japan”. The data are stored in the variable nyt_japan.

search_q <- URLencode("'Japan'")
nyt_japan <- data.frame(year=character(0),hits=numeric(0))

for(year in year_range){
    url <- paste('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=',search_q,'&begin_date=',year,'0101&end_date=',year,'1231&api-key=',api,sep="")
    nyt_robj <- fromJSON(getURL(url))  ## Convert from JSON to R Object
  hits <- nyt_robj$response$meta["hits"]   ## save the article hits
    nyt_japan <- rbind(nyt_japan,data.frame(year=year,hits=hits))   ## Add a new row to nyt_china
    print(paste(year,hits,sep=":"))  ### Print the new row contents
    Sys.sleep(5) ### Wait a while to limit within 1 call per second
}
## [1] "1990:5571"
## [1] "1991:4864"
## [1] "1992:4826"
## [1] "1993:4286"
## [1] "1994:3978"
## [1] "1995:4516"
## [1] "1996:3844"
## [1] "1997:4091"
## [1] "1998:4616"
## [1] "1999:4058"
## [1] "2000:3936"
## [1] "2001:4318"
## [1] "2002:3973"
## [1] "2003:4065"
## [1] "2004:3612"
## [1] "2005:3413"
## [1] "2006:4262"
## [1] "2007:4592"
## [1] "2008:4861"
## [1] "2009:5479"
## [1] "2010:5891"
## [1] "2011:6749"
## [1] "2012:5446"
## [1] "2013:4597"
## [1] "2014:4630"
## [1] "2015:4030"
## [1] "2016:3237"
## [1] "2017:3339"
## [1] "2018:3431"

A function named SearchNYT is defined to search keyword in the NYT archive. No need to duplciate the code again.

SearchNYT <- function(sq){ 
  nyt <- data.frame(year=character(0),hits=numeric(0))
  for(year in year_range){
      url <- paste('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=',sq,'&begin_date=',year,'0101&end_date=',year,'1231&api-key=',api,sep="")
      nyt_robj <- fromJSON(getURL(url))  ## Convert from JSON to R Object
    hits <- nyt_robj$response$meta["hits"]   ## save the article hits
      nyt <- rbind(nyt,data.frame(year=year,hits=hits)) ## Add a new row 
      print(paste(year,hits,sep=":"))  ### Print the new row contents
      Sys.sleep(5) ### Wait a while to limit within 1 call per second
  }
  return(nyt)
}

We search for “North Korea” and “Syria” by calling the defined function SearchNYT.

search_q <- URLencode("'North Korea'")
nyt_nk <- SearchNYT(search_q)
## [1] "1990:208"
## [1] "1991:221"
## [1] "1992:281"
## [1] "1993:347"
## [1] "1994:723"
## [1] "1995:252"
## [1] "1996:305"
## [1] "1997:301"
## [1] "1998:277"
## [1] "1999:465"
## [1] "2000:537"
## [1] "2001:528"
## [1] "2002:831"
## [1] "2003:1107"
## [1] "2004:622"
## [1] "2005:590"
## [1] "2006:853"
## [1] "2007:734"
## [1] "2008:633"
## [1] "2009:1034"
## [1] "2010:954"
## [1] "2011:634"
## [1] "2012:729"
## [1] "2013:894"
## [1] "2014:754"
## [1] "2015:591"
## [1] "2016:755"
## [1] "2017:2174"
## [1] "2018:2174"
search_q <- URLencode("'Syria'")
nyt_sy <- SearchNYT(search_q)
## [1] "1990:682"
## [1] "1991:1016"
## [1] "1992:454"
## [1] "1993:411"
## [1] "1994:389"
## [1] "1995:281"
## [1] "1996:421"
## [1] "1997:241"
## [1] "1998:208"
## [1] "1999:434"
## [1] "2000:530"
## [1] "2001:441"
## [1] "2002:459"
## [1] "2003:783"
## [1] "2004:487"
## [1] "2005:840"
## [1] "2006:1175"
## [1] "2007:950"
## [1] "2008:630"
## [1] "2009:539"
## [1] "2010:509"
## [1] "2011:1621"
## [1] "2012:2476"
## [1] "2013:2627"
## [1] "2014:3225"
## [1] "2015:3594"
## [1] "2016:3055"
## [1] "2017:2569"
## [1] "2018:1953"

Last, all three time series are plotted by lines.

p <- plot_ly(x = nyt_china$year, y = nyt_china$hits, name = "China", type = 'scatter', mode = 'lines')
p <- add_trace(p, y = nyt_japan$hits, name = "Japan")
p <- add_trace(p, y = nyt_nk$hits, name = "North Korea")
p <- add_trace(p, y = nyt_sy$hits, name = "Syria")
layout(p, title = "Country's Names mentioned in New York Times (1990 to 2018)", xaxis = list(title = "Year"), yaxis = list (title = "Number of hits"))

Finally, we upload the plot to the plotly server. You should first create an account here (https://plot.ly/) and obtain the username and the API key.

#### Upload to plotly server
Sys.setenv("plotly_username"="YOUR PLOTLY ACCOUNT NAME")
Sys.setenv("plotly_api_key"="YOUR PLOTLY KEY HERE")
api_create(p, filename = "lecture3")