The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis We will get an API key first to access an article.
The task of this assignement is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it into an R DataFram.
#Load libraries
First, we must obtain the correct libraries.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.1 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.1.0
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.4 ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
library(lubridate)
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
Then we must obtain a API key. This can be done by going to the developer website of the NYTimes and signing up via email. Let’s choose Movie Reviews.
key <- rstudioapi::askForPassword("Authorization Key")
Then lets choose 2 different movie names, one “The Big Lebowski (Movie)” and the other as “Godfather”, more general term for all Godfather movies.
movie_of_interest <-"lebowski"
movie_of_interest2 <-"godfather"
url <- paste0('https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=',movie_of_interest2,'&api-key=', key)
url2 <- paste0('https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=',movie_of_interest,'&api-key=', key)
godfather_data <- fromJSON(url)$results %>%
as.data.frame()
lebowski_data <- fromJSON(url2)$results %>%
as.data.frame()
Here we managed to extract both the godfather query data and the lebowski query data as a json and transformed it into an R data frame.
class(godfather_data)
## [1] "data.frame"
class(lebowski_data)
## [1] "data.frame"
rmarkdown::paged_table(godfather_data)
rmarkdown::paged_table(lebowski_data)
The NYT is a great tool for anyone to access. We’ve managed to get a NYT API key, choose a subject, extract the subject as a json, and finally view it as an R dataframe. This assingment has been great.