library(tidyverse)
## -- Attaching packages ----------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.2
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts -------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(knitr)
library(httr)
library(jsonlite)
##
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
##
## flatten
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 into an R DataFrame.
I went on the NY Times API website and I chose the Books API, since I am a fan of reading books. I chose the “Book Sellers History”. I retrieved info for the history to see the last few best sellers.
ex<-GET("https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?api-key=AkaADUlHKZkrwFyA3iDtzBbhtuZMECIa")
cat("The status returned is:", ex$status_code)
## The status returned is: 200
data <- fromJSON(rawToChar(ex$content))
names(data)
## [1] "status" "copyright" "num_results" "results"
df<-data.frame(data$results, stringsAsFactors = FALSE)
df %>%
select(title, description, author, publisher, reviews) %>%
rename("Title" = title, "Description" = description,"Author" = author, "Publisher" = publisher, "Reviews"=reviews) %>%
kable
| Title | Description | Author | Publisher | Reviews |
|---|---|---|---|---|
| “I GIVE YOU MY BODY …” | The author of the Outlander novels gives tips on writing sex scenes, drawing on examples from the books. | Diana Gabaldon | Dell | , , , |
| “MOST BLESSED OF THE PATRIARCHS” | A character study that attempts to make sense of Jefferson’s contradictions. | Annette Gordon-Reed and Peter S Onuf | Liveright | , , , |
| #ASKGARYVEE | The entrepreneur expands on subjects addressed on his Internet show, like marketing, management and social media. | Gary Vaynerchuk | HarperCollins | , , , |
| #GIRLBOSS | An online fashion retailer traces her path to success. | Sophia Amoruso | Portfolio/Penguin/Putnam | , , , |
| #IMOMSOHARD | Kristin Hensley and Jen Smedley | HarperOne | , , , | |
| #NEVERAGAIN | Students from Marjory Stoneman Douglas High School describe the Valentine’s Day mass shooting and outline ways to prevent similar incidents. | David Hogg and Lauren Hogg | Random House | , , , |
| $100 STARTUP | How to build a profitable start up for $100 or less and be your own boss. | Chris Guillebeau | Crown Business | , , , |
| $20 PER GALLON | Christopher Steiner | Grand Central | , , , | |
| ’57, Chicago | NA | Steve Monroe | NA | , NA , https://www.nytimes.com/2001/07/29/books/books-in-brief-fiction-poetry-319660.html, NA |
| ‘ROCK OF AGES:’‘ROLLING STONE’’ HISTORY OF ROCK AND ROLL’ | NA | GEOFFREY STOKES, KEN TUCKER’ ’ED WARD | NA | , NA , https://www.nytimes.com/1986/12/28/books/three-chord-music-in-a-three-piece-suit.html, NA |
| ‘THE HIGH ROAD TO CHINA: GEORGE BOGLE, THE PANCHEN LAMA AND THE FIRST BRITISH EXPEDITION TO TIBET’ | NA | KATE TELTSCHER | NA | , NA , https://www.nytimes.com/2007/04/22/books/review/Stuart.t.html, NA |
| ’TIL DEATH | Sharon Sala | Harlequin Mira | , , , | |
| ’TIL DEATH DO US PART | A matchmaker in Victorian England turns to a crime novelist for help when she starts receiving a series of disturbingly personalized trinkets. | Amanda Quick | Berkley | , , , |
| ’Til Faith Do Us Part: How Interfaith Marriage is Transforming America | NA | Naomi Schaefer Riley | NA | , NA , https://www.nytimes.com/2013/05/26/books/review/til-faith-do-us-part-by-naomi-schaefer-riley.html, NA |
| ’TIS THE SEASON | Two classic holiday stories — “Under the Christmas Tree” (2009) and “Midnight Confessions” (2010) — plus the novella “Backward Glance” (1991). | Ron Carr | Harlequin Mira | , , , |
| ——, THAT’S DELICIOUS | Action Bronson with Rachel Wharton | Abrams | , , , | |
| …and the Horse He Rode In On: The People V. Kenneth Starr | NA | James Carville | NA | , NA , https://www.nytimes.com/1998/10/18/books/preaching-to-the-converted.html, NA |
| .HACK G.U. , VOL. 5 | This series, set in the future, is about an online, multiplayer game run amok. This volume concludes the tale. | Hamazaki Tatsuya | TOKYOPOP | , , , |
| 1 Ragged Ridge Road | NA | David Adams Richards | NA | , NA , https://www.nytimes.com/1997/08/31/books/books-in-brief-fiction-747866.html, NA |
| 1,000 PLACES TO SEE BEFORE YOU DIE | A guide for traveling the world; second edition updated with new entries. | Patricia Schultz | Workman | , , , |
tf <- is.data.frame(df)
cat("Is this a dataframe?", tf)
## Is this a dataframe? TRUE
I was able to retrieve, format and use the API from The New York Times.