#install.packages("rjson")
#install.packages("httr")
#install.packages("jsonlite")
library(rjson)
library(httr)
## Warning: package 'httr' was built under R version 3.3.3
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 3.3.3
## 
## Attaching package: 'jsonlite'
## The following objects are masked from 'package:rjson':
## 
##     fromJSON, toJSON
#Commenting out this entire section since it doesn't work. Trying a new approach below.

#Query
#query <- "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=dogs+smoking&api-key=d00dcc808b5e48fba640a20b8626655a"

#trying to load the url and API key separately to see if that's causing the unexpected character error. it's not, it's producing the same error
#url <- "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=dogs+smoking"
#APIkey <- "d00dcc808b5e48fba640a20b8626655a"
#data <- fromJSON(paste0(url, APIkey))

#Connect to the query and load the output
#output <- content(GET(url = query))

#Check that it worked
#http_status(output)

#These two operations are erroring out for two different reasons - can't seem to get either of them to work. NEED TO DEBUG

#dogsdf <- fromJSON(output)
#Error in fromJSON(output) : STRING_ELT() can only be applied to a 'character vector', not a 'list'

#dogsdf <- fromJSON(as.character(output))
#Error in fromJSON(as.character(output)) : unexpected character 'l'

API_key <- "&api-key=d00dcc808b5e48fba640a20b8626655a"
url <- "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=dogs+smoking"
data <- fromJSON(paste0(url, API_key))
headlines <- data$response$docs$headline
colnames(headlines)
## [1] "main"           "content_kicker" "kicker"         "print_headline"
## [5] "sub"