Assignment

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

You’ll need to start by signing up for an API key.

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.

Solution

Libraries

library(jsonlite)
library(DT)
library(tidyr)
library(dplyr)

Access NY Times API Data

Get API key from NY Times:

key <- "&api-key=2989168319214883a9a2a5e3d5713d88"

NY Times API used: (/lists/best-sellers/history.json)

url <- "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?age-group=4"

I want to query the bestseller books for 4-year-olds. I tried to include the AgeGroup value in the fromJSON function below, but kept getting error messages. Eventually had to manually add it to the url above to get it work.

AgeGroup <- "?age-group=4"

Use jsonlite [fromJSON] function to get the API data with API key and query for kids books from NY Times:

rawdata <- fromJSON(paste0(url, key))

Reference: https://cran.r-project.org/web/packages/jsonlite/vignettes/json-apis.html

Confirm it worked:

head(rawdata)
## $status
## [1] "OK"
## 
## $copyright
## [1] "Copyright (c) 2017 The New York Times Company.  All Rights Reserved."
## 
## $num_results
## [1] 331
## 
## $results
##                                   title
## 1                 A BAD KITTY CHRISTMAS
## 2               A BIG GUY TOOK MY BALL!
## 3                     A HERO FOR WONDLA
## 4                    A VISITOR FOR BEAR
## 5                   ACROSS THE UNIVERSE
## 6                         ALL THE WORLD
## 7                              AMBITION
## 8      AMELIA BEDELIA'S FIRST VALENTINE
## 9  AMELIA BEDELIA’S FIRST DAY OF SCHOOL
## 10                 AND THEN IT’S SPRING
## 11                          AQUALICIOUS
## 12                   ARTHUR TURNS GREEN
## 13                   ASCENDANCE TRILOGY
## 14                      ASPIRING ARTIST
## 15                      BANE CHRONICLES
## 16                               BARACK
## 17                  BATS AT THE LIBRARY
## 18                       BATTLE AT TETH
## 19                   BEAUTIFUL DISASTER
## 20                           BEE AND ME
##                                                                                     description
## 1                                             Bad Kitty learns the true meaning of the holiday.
## 2                                               Someone stole Piggie's ball. That is not right.
## 3                                                   Something sinister threatens planet Orbona.
## 4                                               A mouse keeps popping in on an antisocial bear.
## 5                     The spaceship Godspeed travels to a new Earth carrying 100 frozen people.
## 6                                                  A day in the life of a multicultural family.
## 7  The headmaster wants to shut down Billings Hall, and Reed needs to save it; a Private novel.
## 8                                                          Hearts abound, even on French fries.
## 9                                                                    Amelia enters first grade.
## 10                                                                           Planting a garden.
## 11                                                       Pinkalicious helps a mermaid get home.
## 12                                                 Arthur explores ways to save energy at home.
## 13                                          The kingdom of Carthya is on the brink of disaster.
## 14                                            Fancy Nancy puts her trademark flair to good use.
## 15                         Linked stories about Magnus Bane, a warlock with a mysterious past. 
## 16                                                                             The Obama story.
## 17                                       There’s a lot they can do besides hanging upside down.
## 18                   Clones vs. droids; a “Star Wars” book based on the movie “The Clone Wars.”
## 19                                            From a mental institution back to a charmed life.
## 20                                                                            Why we need bees.
##                                                   contributor
## 1                                               by Nick Bruel
## 2                                               by Mo Willems
## 3                                          by Tony DiTerlizzi
## 4       by Bonny Becker. Illustrated by Kady MacDonald Denton
## 5                                               by Beth Revis
## 6         by Liz Garton Scanlon. Illustrated by  Marla Frazee
## 7                                               by Kate Brian
## 8                by Herman Parish. Illustrated by Lynne Avril
## 9                by Herman Parish. Illustrated by Lynne Avril
## 10            by Julie Fogliano. Illustrated by Erin E. Stead
## 11                                           by Victoria Kann
## 12                                              by Marc Brown
## 13                                     by Jennifer A. Nielsen
## 14      by Jane O'Connor. Illustrated by Robin Preiss Glasser
## 15 by Cassandra Clare, Maureen Johnson and Sarah Rees Brennan
## 16                    by Jonah Winter. Illustrated by Ag Ford
## 17                      written and illustrated by Brian Lies
## 18                                           by Kirsten Mayer
## 19                                              by Kate Brian
## 20        by Elle J. McGuinness. Illustrated by Heather Brown
##                                                     author
## 1                                               Nick Bruel
## 2                                               Mo Willems
## 3                                          Tony DiTerlizzi
## 4                                             Bonny Becker
## 5                                               Beth Revis
## 6                                       Liz Garton Scanlon
## 7                                               Kate Brian
## 8                                            Herman Parish
## 9                                            Herman Parish
## 10                                          Julie Fogliano
## 11                                           Victoria Kann
## 12                                              Marc Brown
## 13                                      Jennifer A Nielsen
## 14                                           Jane O'Connor
## 15 Cassandra Clare, Maureen Johnson and Sarah Rees Brennan
## 16                                            Jonah Winter
## 17                                              Brian Lies
## 18                                           Kirsten Mayer
## 19                                              Kate Brian
## 20                                       Elle J McGuinness
##                         contributor_note price      age_group
## 1                                        15.99    Ages 4 to 8
## 2                                         0.00    Ages 4 to 8
## 3                                        17.99  Ages 10 to 14
## 4   Illustrated by Kady MacDonald Denton 16.99    Ages 4 to 8
## 5                                        17.99 Ages 14 and up
## 6           Illustrated by  Marla Frazee 17.99    Ages 4 to 8
## 7                                         9.99 Ages 14 and up
## 8             Illustrated by Lynne Avril 16.99    Ages 4 to 8
## 9             Illustrated by Lynne Avril 16.99    Ages 4 to 8
## 10          Illustrated by Erin E. Stead 16.99    Ages 4 to 7
## 11                                        0.00    Ages 4 to 8
## 12                                       16.99    Ages 4 to 8
## 13                                        0.00  Ages 10 to 14
## 14   Illustrated by Robin Preiss Glasser 12.99    Ages 4 to 7
## 15                                        0.00 Ages 14 and up
## 16                Illustrated by Ag Ford 17.99    Ages 4 to 7
## 17 written and illustrated by Brian Lies 16.00    Ages 4 to 8
## 18                                        3.99    Ages 4 to 8
## 19                                        9.99 Ages 14 and up
## 20          Illustrated by Heather Brown 16.99    Ages 4 to 8
##                      publisher
## 1    Neal Porter/Roaring Brook
## 2              Disney-Hyperion
## 3             Simon & Schuster
## 4                   Candlewick
## 5                    Razorbill
## 6             Simon & Schuster
## 7                  Simon Pulse
## 8    Greenwillow/HarperCollins
## 9                  Greenwillow
## 10   Neal Porter/Roaring Brook
## 11        Harper/HarperCollins
## 12               Little, Brown
## 13                  Scholastic
## 14        Harper/HarperCollins
## 15            Simon & Schuster
## 16       Collins/HarperCollins
## 17            Houghton Mifflin
## 18 Grosset & Dunlap/LucasBooks
## 19            Simon & Schuster
## 20       Accord/Andrews McMeel
##                                                                                                                                                               isbns
## 1                                                                                                                                         1596436689, 9781596436688
## 2                                                                                                                                         1423174917, 9781423174912
## 3                                                                                                                                         1416983120, 9781416983125
## 4                                                                                                                                                              NULL
## 5                                                                                                                                         1595143971, 9781595143976
## 6                                                                                                                                         1416985808, 9781416985808
## 7                                                                                                                                                              NULL
## 8                                                                                                              0061544582, 0062032755, 9780061544583, 9780062032751
## 9                                                                                                                                                              NULL
## 10                                                                                                                                        1596436247, 9781596436244
## 11                                                                                                                                        0062330160, 9780062330161
## 12                                                                                                                                        0316129240, 9780316129244
## 13 0545284139, 0545284147, 0545284171, 0545633796, 0545284155, 0545284163, 9780545284134, 9780545284141, 9780545284172, 9780545633796, 9780545284158, 9780545284165
## 14                                                                                                                                        0061915262, 9780061915260
## 15 144249557X, 144249560X, 1442495626, 1442495642, 1442495634, 1442495995, 9781442495579, 9781442495609, 9781442495623, 9781442495647, 9781442495630, 9781442495999
## 16                                                                                                                                                             NULL
## 17                                                                                                                                                             NULL
## 18                                                                                                                                                             NULL
## 19                                                                                                                                                             NULL
## 20                                                                                                                                                             NULL
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ranks_history
## 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
## 2  1423174917, 1423174917, 1423174917, 1423174917, 1423174917, 1423174917, 1423174917, 1423174917, 9781423174912, 9781423174912, 9781423174912, 9781423174912, 9781423174912, 9781423174912, 9781423174912, 9781423174912, 10, 9, 10, 8, 10, 8, 8, 8, Picture Books, Picture Books, Picture Books, Picture Books, Picture Books, Picture Books, Picture Books, Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, 2015-01-25, 2013-10-06, 2013-09-15, 2013-09-01, 2013-08-25, 2013-08-18, 2013-08-11, 2013-08-04, 2015-01-10, 2013-09-21, 2013-08-31, 2013-08-17, 2013-08-10, 2013-08-03, 2013-07-27, 2013-07-20, 16, 15, 14, 13, 12, 11, 10, 9, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## 3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
## 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
## 5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
## 6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
## 7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
## 8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
## 9                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NULL
## 10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     NULL
## 11                                                                                                                                                                                                                                                                                                                                               0062330160, 0062330160, 0062330160, 0062330160, 0062330160, 9780062330161, 9780062330161, 9780062330161, 9780062330161, 9780062330161, 5, 5, 5, 4, 1, Picture Books, Picture Books, Picture Books, Picture Books, Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, Children’s Picture Books, 2015-04-19, 2015-04-12, 2015-04-05, 2015-03-29, 2015-03-22, 2015-04-04, 2015-03-28, 2015-03-21, 2015-03-14, 2015-03-07, 5, 4, 3, 2, 1, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## 12                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     NULL
## 13                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       0545284171, 9780545284172, 7, Series Books, Children’s Series, 2014-03-16, 2014-03-01, 1, NA, 0, 0
## 14                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     NULL
## 15                                                                                                                                                                                                                                                                                   1442495995, 1442495995, 1442495634, 1442495642, 1442495626, 144249560X, 9781442495999, 9781442495999, 9781442495630, 9781442495647, 9781442495623, 9781442495609, 9, 7, 8, 8, 9, 6, Series Books, Series Books, Series Books, Series Books, Series Books, Series Books, Children’s Series, Children’s Series, Children’s Series, Children’s Series, Children’s Series, Children’s Series, 2014-12-07, 2014-11-30, 2014-04-06, 2013-12-08, 2013-11-03, 2013-09-08, 2014-11-22, 2014-11-15, 2014-03-22, 2013-11-23, 2013-10-19, 2013-08-24, 7, 6, 5, 4, 3, 2, NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## 16                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     NULL
## 17                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     NULL
## 18                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     NULL
## 19                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     NULL
## 20                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     NULL
##                                                                   reviews
## 1                                                                  , , , 
## 2                                                                  , , , 
## 3                                                                  , , , 
## 4                                                                  , , , 
## 5                                                                  , , , 
## 6       , , https://www.nytimes.com/2009/11/08/books/review/Bast-t.html, 
## 7                                                                  , , , 
## 8                                                                  , , , 
## 9                                                                  , , , 
## 10                                                                 , , , 
## 11                                                                 , , , 
## 12                                                                 , , , 
## 13                                                                 , , , 
## 14                                                                 , , , 
## 15                                                                 , , , 
## 16                                                                 , , , 
## 17 , , https://www.nytimes.com/2008/09/14/books/review/Bookshelf-t.html, 
## 18                                                                 , , , 
## 19                                                                 , , , 
## 20                                                                 , , ,

Yay!

Review raw data

What class are we dealing with:

class(rawdata) 
## [1] "list"

How many elements?

length(rawdata)
## [1] 4

Names?

names(rawdata)
## [1] "status"      "copyright"   "num_results" "results"

Class of each element?

lapply(rawdata, class)
## $status
## [1] "character"
## 
## $copyright
## [1] "character"
## 
## $num_results
## [1] "integer"
## 
## $results
## [1] "data.frame"

How many elements in each list component?

lapply(rawdata, length)
## $status
## [1] 1
## 
## $copyright
## [1] 1
## 
## $num_results
## [1] 1
## 
## $results
## [1] 11

Clean the data before transforming into dataframe

Remove the first 3 lists (“status”, “copyright”, “num_results”):

results <- rawdata[-c(1:3)]
names(results)
## [1] "results"
length(results)
## [1] 1
class(results)
## [1] "list"

Turn into a dataframe

results_df <- results[["results"]]

Confirm:

class(results_df)
## [1] "data.frame"

Yay!

Tidy data with [dpylr] and [tidyr] packages

colnames(results_df)
##  [1] "title"            "description"      "contributor"     
##  [4] "author"           "contributor_note" "price"           
##  [7] "age_group"        "publisher"        "isbns"           
## [10] "ranks_history"    "reviews"
kidsbooks <- tbl_df(results_df)
kidsbooks <- select(kidsbooks, one_of(c("title", "description", "author", "price", "age_group")))
kidsbooks <- rename(kidsbooks, Title = 'title', Description = 'description', Author = 'author', Price = 'price', Ages = 'age_group')
kidsbooks
## # A tibble: 20 x 5
##                                   Title
##  *                                <chr>
##  1                A BAD KITTY CHRISTMAS
##  2              A BIG GUY TOOK MY BALL!
##  3                    A HERO FOR WONDLA
##  4                   A VISITOR FOR BEAR
##  5                  ACROSS THE UNIVERSE
##  6                        ALL THE WORLD
##  7                             AMBITION
##  8     AMELIA BEDELIA'S FIRST VALENTINE
##  9 AMELIA BEDELIA’S FIRST DAY OF SCHOOL
## 10                 AND THEN IT’S SPRING
## 11                          AQUALICIOUS
## 12                   ARTHUR TURNS GREEN
## 13                   ASCENDANCE TRILOGY
## 14                      ASPIRING ARTIST
## 15                      BANE CHRONICLES
## 16                               BARACK
## 17                  BATS AT THE LIBRARY
## 18                       BATTLE AT TETH
## 19                   BEAUTIFUL DISASTER
## 20                           BEE AND ME
## # ... with 4 more variables: Description <chr>, Author <chr>, Price <dbl>,
## #   Ages <chr>

Make a Pretty Table

datatable(kidsbooks)