Slection of the libraries:
library(httr)
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(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1 ✔ readr 1.3.1
## ✔ tibble 2.1.3 ✔ purrr 0.3.2
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ ggplot2 3.2.1 ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ purrr::flatten() masks jsonlite::flatten()
## ✖ dplyr::lag() masks stats::lag()
The code to extract the articles based on the mention of HIV in them from the dates of Jan 01, 2016 to Oct 31, 2019.
apikey <- '1pHIViwXgD31sA9yA5uvfIkyi8wv7Zn0'
path <- "http://api.nytimes.com/svc/search/v2/articlesearch.json"
# install.packages("httr")
library(httr)
r <- GET(path, query=list(q="HIV",
"api-key"=apikey,
"begin_date"=20160101,
"end_date"=20191031))
response <- content(r, as = "text", encoding = "UTF-8")
Creation of a dataframe using from JSON:
df <- fromJSON(response, flatten = TRUE) %>%
data.frame()
head(df)
nrow(df)
## [1] 10
Selection and renaming of columns of interest:
df2 <- select(df, Date = response.docs.pub_date, Source = response.docs.source,
Section = response.docs.section_name, "Word Count" = response.docs.word_count, URL =
response.docs.web_url)
df2