R Markdown

NYTimes APIKey = hvHeqqlTzIhjv5GtM0M4WQo5isCzHQ1l

NYTimes AppId = 3c15af75-0c16-4bbd-8517-d5654e6f93df

Example call: https://api.nytimes.com/svc/search/v2/articlesearch.json

Referecen: https://datascienceplus.com/accessing-web-data-json-in-r-using-httr/

#install.packages(c("httr", "jsonlite", "lubridate"))
library(httr)
## Warning: package 'httr' was built under R version 3.5.3
#This package exposes some additional functions to convert json/text to data frame
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 3.5.3
#This library is used to manipulate data
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 3.5.3
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
apiKey = "hvHeqqlTzIhjv5GtM0M4WQo5isCzHQ1l"
url  <- "http://api.nytimes.com/svc/search/v2/articlesearch.json"

result <- GET(url, query=list(q="climate","api-key"=apiKey))
result
## Response [http://api.nytimes.com/svc/search/v2/articlesearch.json?q=climate&api-key=hvHeqqlTzIhjv5GtM0M4WQo5isCzHQ1l]
##   Date: 2019-10-14 11:33
##   Status: 200
##   Content-Type: application/json;charset=UTF-8
##   Size: 194 kB
#For debugging to see if we have data
#content(result, 'text')
#Was there an error in retrieval of data
#http_type(result)

nytdata <- content(result, as = "parsed") 
#nytdata

modJson <- nytdata$response$docs #. Access data element of whole list and ignore other vectors
#modJson
#What is the type
#class(modJson)

#Convert to an array
arr <- array(modJson)
#arr

#Get number of items in list
iterations <- length(modJson)
#For debugging iterations

#Establish matrix to construct our data frame 
m <- matrix(ncol = 11, nrow = iterations)

row <- 1

#Construct matrix and populate with list items 
for(i in 1:iterations){
  for ( j in arr[i]) { 
    if(j$web_url != "")
      m[row, 1] <- (j$`web_url`)
    if(j$snippet != "")
      m[row, 2] <- j$snippet
    if(j$lead_paragraph != "")
      m[row, 3] <- j$lead_paragraph
    if(j$abstract != "")
      m[row, 4] <- j$abstract
    if(j$source != "")
      m[row, 5] <- j$source
    if(!is.null(j$headline[1]$main))
      m[row, 6] <- j$headline[1]$main

    if(!is.null(j$pub_date))
      m[row, 7] <- j$pub_date

    if(!is.null(j$document_type))
      m[row, 8] <- j$document_type
    
    if(!is.null(j$news_desk))
      m[row, 9]  <- j$news_desk
    
    if(!is.null(j$word_count))
      m[row, 10] <- j$word_count
    
    if(!is.null(j$type_of_material))
      m[row, 11] <- j$type_of_material
    row <- row + 1
    
  }
}
#m

z3 <- data.frame(m)
x <- c('web_url','snippet','lead_paragraph','abstract', 'source', 'main', 'pub_date', 'document_type', 'news_desk', 'word_count', 'type_of_material')
colnames(z3) <- x
z3 %>% kable() %>%  kable_styling()
web_url snippet lead_paragraph abstract source main pub_date document_type news_desk word_count type_of_material
https://www.nytimes.com/2019/09/25/climate/nyt-newsletter-climate-strikes-protests.html Also in this edition, community solar projects and a fuzzy pledge from businesses. Welcome to the Climate Fwd: newsletter. The New York Times climate team emails readers once a week with stories and insights about climate change. Sign up here to get it in your inbox. Also in this edition, community solar projects and a fuzzy pledge from businesses. The New York Times A Week of Climate Action, and Inaction 2019-09-25T17:58:39+0000 article Climate 1359 News
https://www.nytimes.com/2019/10/09/climate/children-anxiety-climate-newsletter-nyt.html Also this week, how we captured the stark beauty of ghost forests. Welcome to the Climate Fwd: newsletter. The New York Times climate team emails readers once a week with stories and insights about climate change. Sign up here to get it in your inbox. Also this week, how we captured the stark beauty of ghost forests. The New York Times One Thing You Can Do: Talk to Your Children About Climate Change 2019-10-09T16:29:21+0000 article Climate 862 News
https://www.nytimes.com/2019/07/10/climate/nyt-climate-newsletter-cei.html Also this week, why you should talk about global warming Welcome to the Climate Fwd: newsletter. The New York Times climate team emails readers once a week with stories and insights about climate change. Sign up here to get it in your inbox. Also this week, why you should talk about global warming The New York Times Following the Money That Undermines Climate Science 2019-07-10T17:14:28+0000 article Climate 863 News
https://www.nytimes.com/2019/09/23/climate/climate-summit-global-warming.html China made no new commitments at United Nations climate talks to take stronger action. The United States said nothing at all, and a host of other countries made incremental promises at best. UNITED NATIONS — The United Nations Climate Action Summit on Monday was meant to highlight concrete promises by presidents, prime ministers and corporate executives to wean the global economy from fossil fuels to avoid the worst effects of global warming. China made no new commitments at United Nations climate talks to take stronger action. The United States said nothing at all, and a host of other countries made incremental promises at best. The New York Times At U.N. Climate Summit, Few Commitments and U.S. Silence 2019-09-23T14:28:20+0000 article Climate 1238 News
https://www.nytimes.com/2019/10/02/climate/nyt-climate-newsletter-laundry.html Also this week, news about meat and what it means for climate. Welcome to the Climate Fwd: newsletter. The New York Times climate team emails readers once a week with stories and insights about climate change. Sign up here to get it in your inbox. (And find the website version of this week’s letter here.) Also this week, news about meat and what it means for climate. The New York Times One Thing You Can Do: Smarter Laundry 2019-10-02T19:06:38+0000 article Climate 1241 News
https://www.nytimes.com/2019/08/28/climate/one-thing-we-can-do-drive-less.html Also this week, Greta Thunberg arrives in New York. Welcome to the Climate Fwd: newsletter. The New York Times climate team emails readers once a week with stories and insights about climate change. Sign up here to get it in your inbox. Also this week, Greta Thunberg arrives in New York. The New York Times One Thing We Can Do: Drive Less 2019-08-28T18:24:28+0000 article Climate 852 News
https://www.nytimes.com/2019/08/29/climate/climate-rule-trump-reversing.html The Trump administration is trying to roll back more than 80 environmental rules; half involve climate change. Here are some of the biggest climate-related ones. The move to rescind environmental rules governing emissions of methane, a powerful greenhouse gas, brings to 84 the total number of environmental rules that the Trump administration has worked to repeal. Officials at the White House, the Environmental Protection Agency and other agencies have called the regulations burdensome to the fossil fuel industry and other businesses. The Trump administration is trying to roll back more than 80 environmental rules; half involve climate change. Here are some of the biggest climate-related ones. The New York Times Major Climate Change Rules the Trump Administration Is Reversing 2019-08-29T18:32:15+0000 article Climate 841 News
https://www.nytimes.com/2019/05/01/climate/nyt-climate-newsletter-food.html Also this week, a quiz to help you assess your diet’s climate impact. Welcome to the Climate Fwd: newsletter. The New York Times climate team emails readers once a week with stories and insights about climate change. Sign up here to get it in your inbox. Also this week, a quiz to help you assess your diet’s climate impact. The New York Times The Facts About Food and Climate Change 2019-05-01T18:05:17+0000 article Climate 546 News
https://www.nytimes.com/2019/06/05/climate/nyt-climate-newsletter-biden.html Also this week, leisure travel on a warming planet. Welcome to the Climate Fwd: newsletter. The New York Times climate team emails readers once a week with stories and insights about climate change. Sign up here to get it in your inbox. Also this week, leisure travel on a warming planet. The New York Times What Biden’s Climate Plan Shows About the Democratic Field 2019-06-05T15:23:43+0000 article Climate 792 News
https://www.nytimes.com/2019/09/27/climate/climate-change-protests-funding.html Three philanthropists started the Climate Emergency Fund earlier this year, grants from which help climate change protestors spread their message. Climate change protesters from Extinction Rebellion snarled traffic in Washington on Monday and again on Friday. You might find yourself asking, “Who helps pays for this activism?” Three philanthropists started the Climate Emergency Fund earlier this year, grants from which help climate change protestors spread their message. The New York Times Meet the Millionaires Helping to Pay for Climate Protests 2019-09-27T10:00:11+0000 article Climate 1034 News