Assignment
Pick a New York Times API, construct an interface in R to read in the JSON data, and transform it to an R dataframe.
Load Libraries
library(httr)
library(rjson)
library(jsonlite)
library(dplyr)Get NYT Articles Content using API Key
nytimes_articles <- GET("https://api.nytimes.com/svc/search/v2/articlesearch.json",
query = list(api_key = "5bc9e7cba56a4e2fb0fe6bd8ba7ccd97"))
names(nytimes_articles)## [1] "url" "status_code" "headers" "all_headers" "cookies"
## [6] "content" "date" "times" "request" "handle"
Transform into Data Frame
jsondf <- fromJSON(nytimes_articles$url) %>% as.data.frame()
head(jsondf,1)## status
## 1 OK
## copyright
## 1 Copyright (c) 2017 The New York Times Company. All Rights Reserved.
## response.docs.web_url
## 1 https://www.nytimes.com/aponline/2017/10/28/sports/tennis/ap-ten-swiss-indoors.html
## response.docs.snippet
## 1 Roger Federer cruised past David Goffin 6-1, 6-2 in the Swiss Indoors semifinals on Saturday to reach an 11th straight final when playing at his hometown event.
## response.docs.source response.docs.multimedia
## 1 AP NULL
## response.docs.headline.main
## 1 Federer Advances to Swiss Indoors Final Against Del Potro
## response.docs.headline.print_headline
## 1 Federer Advances to Swiss Indoors Final Against Del Potro
## response.docs.headline.kicker response.docs.keywords
## 1 <NA> NULL
## response.docs.pub_date response.docs.document_type
## 1 2017-10-28T16:09:38+0000 article
## response.docs.new_desk response.docs.section_name
## 1 None Tennis
## original response.docs.type_of_material
## 1 By THE ASSOCIATED PRESS News
## response.docs._id response.docs.word_count response.docs.score
## 1 59f4abcd95d0e0246f212b1c 114 1
## response.docs.uri response.meta.hits
## 1 nyt://article/263eb39d-f35f-5dff-856d-0eb637d6890a 15384507
## response.meta.offset response.meta.time
## 1 0 638