Our 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.
if (!require('jsonlite')) install.packages('jsonlite')
if (!require('dplyr')) install.packages('dplyr')
if (!require('stringr')) install.packages('stringr')
library('DT')
We will be using Top Stories V1 API and look for stories in the Technology section.
I stored API developer Key in a .R file and sourced accordingly in the code. This file won’t be checked in to the git repository
# get our API key
source("key.R")
#key <- "8ba80c2191ec4062aa14565fb5adab43" #API Key
# add text required for call
addkey <- paste0("&api-key=", key)
Get call to the API endpoint and get the feed
# add url and text required for call
url <- "https://api.nytimes.com/svc/topstories/v1/technology.json"
#url<- "https://developer.nytimes.com/most_popular_api_v2.json"
addurl <- paste0(url, "?")
# make call # ERROR IN THIS STAGE#
feed <- fromJSON(paste0(addurl, addkey))
sub <- feed$results
sub$pub_date <- unlist(str_extract(sub$published_date, ".+?(?=T)"))
sub$pub_time <- unlist(str_extract(sub$published_date, "(?<=T)(.*)"))
sub <- sub %>%
filter(section == "Movies") %>%
select(title, abstract, byline, url, pub_date, pub_time)
## Warning: package 'bindrcpp' was built under R version 3.3.3
knitr::kable(sub)
title | abstract | byline | url | pub_date | pub_time |
---|---|---|---|---|---|
20th Century Fox Used Fake News to Publicize ‘A Cure for Wellness’ | Many users thought articles on sites resembling city newspapers or HealthCare.gov were real exposés or scoops, like “Trump and Putin Spotted at Swiss Resort.” | By LIAM STACK | https://www.nytimes.com/2017/02/15/arts/fake-news-a-cure-for-wellness-movie.html | 2017-02-15 | 08:41:27-5:00 |