The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs You’ll need to start by signing up for an API key. Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it to an R dataframe.
library(jsonlite)
library (knitr)
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(DT)
Search for articles using keywords
article_key <- "&api-key=b75da00e12d54774a2d362adddcc9bef"
url <- "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=obamacare+socialism"
req <- jsonlite::fromJSON(paste0(url, article_key))
articles <- req$response$docs
colnames(articles)
## [1] "web_url" "snippet" "lead_paragraph"
## [4] "abstract" "print_page" "source"
## [7] "multimedia" "headline" "keywords"
## [10] "pub_date" "document_type" "news_desk"
## [13] "section_name" "byline" "type_of_material"
## [16] "_id" "word_count" "uri"
## [19] "subsection_name"
Viewing the JSON dataframe Showing the first six columns
articles <- as.data.frame(articles)
datatable(articles[, 1:6])
I’m just trying another API Search for bestsellers Books
books_key <- "&api-key=76363c9e70bc401bac1e6ad88b13bd1d"
url <- "http://api.nytimes.com/svc/books/v3/lists/overview.json?published_date=2019-01-01"
req <- jsonlite::fromJSON(paste0(url, books_key))
bestsellers <- req$results$list
Create format for the JSON dataframe
category1 <- bestsellers[[1, "books"]]
colnames(category1)
## [1] "age_group" "amazon_product_url" "article_chapter_link"
## [4] "author" "book_image" "book_image_width"
## [7] "book_image_height" "book_review_link" "contributor"
## [10] "contributor_note" "created_date" "description"
## [13] "first_chapter_link" "price" "primary_isbn10"
## [16] "primary_isbn13" "book_uri" "publisher"
## [19] "rank" "rank_last_week" "sunday_review_link"
## [22] "title" "updated_date" "weeks_on_list"
## [25] "buy_links"
subset(category1, select = c("title", "author", "publisher", "rank"))
## title author publisher rank
## 1 THE RECKONING John Grisham Doubleday 1
## 2 EVERY BREATH Nicholas Sparks Grand Central 2
## 3 FIRE AND BLOOD George RR Martin Bantam 3
## 4 WHERE THE CRAWDADS SING Delia Owens Putnam 4
## 5 TARGET: ALEX CROSS James Patterson Little, Brown 5