a_9: Web APIs

Jie Zou

2021-04-05

Load packages

  • httr: interact with web api
  • jsonlite: work with .json file
library(httr)
library(tidyverse)
library(jsonlite)
library(DT)

Http request

check if web api process well

url <- "https://api.nytimes.com/svc/movies/v2/critics/all.json?api-key=somr1GPKGZfJ1wfSsgkLuqNj8YX8GYuR"
http_request <- GET(url)

http_request
## Response [https://api.nytimes.com/svc/movies/v2/critics/all.json?api-key=somr1GPKGZfJ1wfSsgkLuqNj8YX8GYuR]
##   Date: 2021-04-05 20:52
##   Status: 200
##   Content-Type: application/json; charset=utf-8
##   Size: 13.9 kB

Load JSON

load json into data frame and show the rows and columns

movies <- fromJSON(url) %>%
  with(results) %>%
  select(-c(multimedia,bio))

glimpse(movies)
## Rows: 82
## Columns: 4
## $ display_name <chr> "A. O. Scott", "Renata Adler", "Robert Alden", "Eugene Ar…
## $ sort_name    <chr> "A. O. Scott", "Adler, Renata", "Alden, Robert", "Archer,…
## $ status       <chr> "full-time", "part-time", "", "part-time", "part-time", "…
## $ seo_name     <chr> "A-O-Scott", "Renata-Adler", "Robert-Alden", "Eugene-Arch…

Little analysis

what is the ratio of part time vs. full time?

there are some observations without values, it may cause the skewness when calculating the ratio, so I’m going to remove the empties.

# remove empty observations
status <- movies %>%
  select(status) %>%
  filter(status != "")

# calculate the proportions
status <- status %>%
  count(status) %>%
  mutate(ratio = round(n/sum(n), 4))

datatable(status)

Conclusion

There are only 5.08% of critics are full time, and up to 94.92% are part time critics.