Assignment Overview

The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs

Your 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.

I chose to extract the latest bestselling nonfiction paperbacks from the Books API.

Load Libraries

library(jsonlite) #To read JSON files
library(dplyr) #To transform dataframes
library(kableExtra) #To create HTML data table

Import Data

#Import url and API key
url <- 'http://api.nytimes.com/svc/books/v3/lists/current/paperback-nonfiction.json'
apiKey <- 'KQEp4vUctvgrBNlJ2NbWhqAfjyPQkqhf'

#Construct full url
url <- paste0(url, '?api-key=', apiKey)

#Read lines from url
books <- readLines(url, warn = FALSE)

#Read data in JSON format
books <- fromJSON(books)

Create Dataframe

booksDf <- books$results

booksDf2 <- booksDf$books

#Create a subset of dataframe
booksDf3 = booksDf2[c("rank","weeks_on_list","title", "author", "primary_isbn13", "publisher", "description")]

#Rename columns
colnames(booksDf3) = c("Rank", "Weeks On List", "Title", "Author", "ISBN", "Publisher", "Description")

#Manipulate table and display
booksDf3 %>% 
  kable() %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
  scroll_box(width = "100%", height = "450px")
Rank Weeks On List Title Author ISBN Publisher Description
1 6 BORN A CRIME Trevor Noah 9780399588198 Spiegel & Grau A memoir about growing up biracial in apartheid South Africa by the host of “The Daily Show.”
2 45 SAPIENS Yuval Noah Harari 9780062316110 Harper Perennial How Homo sapiens became Earth’s dominant species.
3 37 WHITE FRAGILITY Robin DiAngelo 9780807047415 Beacon Press Historical and cultural analyses on what causes defensive moves by white people and how this inhibits cross-racial dialogue.
4 22 THE BODY KEEPS THE SCORE Bessel van der Kolk 9780143127741 Penguin How trauma affects the body and mind, and innovative treatments for recovery.
5 51 KILLERS OF THE FLOWER MOON David Grann 9780307742483 Vintage The story of a murder spree in 1920s Oklahoma that targeted Osage Indians, whose lands contained oil. The fledgling F.B.I. intervened, ineffectively.
6 1 PLEASURE ACTIVISM edited adrienne maree brown 9781849353267 AK Essays and conversations on the possibilities of making social justice work more gratifying.
7 37 HILLBILLY ELEGY JD Vance 9780062300553 Harper A Yale Law School graduate looks at the struggles of the white working class through the story of his own childhood.
8 30 GRIT Angela Duckworth 9781501111112 Scribner The MacArthur Fellow argues that passion and perseverance are more important than innate talent in creating success.
9 4 I’LL BE GONE IN THE DARK Michelle McNamara 9780062319791 Harper Perennial The late true-crime journalist’s search for “the Golden State Killer.”
10 1 UTOPIA FOR REALISTS Rutger Bregman 9780316471916 Back Bay A guide exploring universal basic income, a 15-hour workweek and open borders across the globe.
11 36 SHOE DOG Phil Knight 9781501135927 Scribner A memoir by the co-founder of Nike, Inc.
12 145 JUST MERCY Bryan Stevenson 9780812984965 Spiegel & Grau A law professor and MacArthur grant recipient’s memoir of his decades of work to free innocent people condemned to death.
13 54 THE INNOCENT MAN John Grisham 9780385340915 Bantam/Dell Grisham’s first nonfiction book concerns a man wrongly sentenced to death.
14 207 THINKING, FAST AND SLOW Daniel Kahneman 9780374533557 Farrar, Straus & Giroux When we can and cannot trust our intuitions in making business and personal decisions.
15 76 BEING MORTAL Atul Gawande 9781250076229 Picador The surgeon and New Yorker writer considers how doctors fail patients at the end of life, and how they can do better.