Libraries

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
## 
##     flatten
library(stringr)
library(ggplot2)

Connect API

My 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 into an R DataFrame.

apikey = "Asgj6WXAv1I2STAdkAM3sV7S352LGAWw"
 
apiurl <- paste("https://api.nytimes.com/svc/topstories/v2/science.json?api-key=", apikey , sep='')

scienceData <- fromJSON(apiurl) 

scienceData <- scienceData %>%
  as.data.frame() %>%
  select(-results.multimedia)
 

head(scienceData)

Analyze

To count different topic in science.

ggplot(scienceData, aes(x=results.section)) + 
  geom_bar()

Conclusion

It is helpful to do research when we can read API from the website.