The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs . The goal of the Week 9 assignment is to use 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.

library(ggplot2)
library(knitr)
library(tidyjson)
library(sqldf)
library(RCurl)
library(dplyr)
library(tidyr)

Construct the Request URI using the Books API.

api_key <- "b72e3548831c4431a88db5b27ad33d87"
main_url <- "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json"
request <- getURL(paste0(main_url,'?api-key=', api_key))

Below shows the structure of the Books API json response.It illustrate the json format.

According to the above format ,using tidyjson I extracted key information from the JSON structure using the pipeline operator %>%. In this case, the JSON structure is complex. In this particular case, the “books_df” object was created, but when entered to “ranks_history” object it isterted to pull multiple rows because of different “published_date”, “primary_isbn13”, then I comented them .

Creating an aggregated data frame from the above to find the publishers popularity. And plot them in a graph

books_df <- request %>% 
    spread_values(status = jstring("status"))  %>%
    enter_object("results") %>%             # Look at their cart
      gather_array("document.id") %>%
       spread_values(book_names = jstring("title"), 
                description = jstring("description"),
                author = jstring("author"),
                price =jnumber("price"),
                publisher = jstring("publisher"))
        # enter_object("ranks_history") %>% 
        # gather_array("document.id") %>%
        # spread_values(published_on = jstring("published_date"),
        #               rank = jnumber("rank"),
        #               ISBN = jstring("primary_isbn13"))
kable(books_df)
document.id status book_names description author price publisher
1 OK “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 0.00 Dell
2 OK “MOST BLESSED OF THE PATRIARCHS” A character study that attempts to make sense of Jefferson’s contradictions. Annette Gordon-Reed and Peter S Onuf 0.00 Liveright
3 OK #ASKGARYVEE The entrepreneur expands on subjects addressed on his Internet show, like marketing, management and social media. Gary Vaynerchuk 0.00 HarperCollins
4 OK #GIRLBOSS An online fashion retailer traces her path to success. Sophia Amoruso 0.00 Portfolio/Penguin/Putnam
5 OK $100 STARTUP How to build a profitable start up for $100 or less and be your own boss. Chris Guillebeau 23.00 Crown Business
6 OK $20 PER GALLON Christopher Steiner 0.00 Grand Central
7 OK ’57, Chicago NA Steve Monroe 0.00 NA
8 OK ‘ROCK OF AGES:’‘ROLLING STONE’‘HISTORY OF ROCK AND ROLL’ NA GEOFFREY STOKES, KEN TUCKER’ ’ED WARD 0.00 NA
9 OK ‘THE HIGH ROAD TO CHINA: GEORGE BOGLE, THE PANCHEN LAMA AND THE FIRST BRITISH EXPEDITION TO TIBET’ NA KATE TELTSCHER 0.00 NA
10 OK ’TIL DEATH Sharon Sala 0.00 Harlequin Mira
11 OK ’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 0.00 Berkley
12 OK ’Til Faith Do Us Part: How Interfaith Marriage is Transforming America NA Naomi Schaefer Riley 0.00 NA
13 OK ’TIS THE SEASON Two classic holiday stories — “Under the Christmas Tree” (2009) and “Midnight Confessions” (2010) — plus the novella “Backward Glance” (1991). Ron Carr 0.00 Harlequin Mira
14 OK …and the Horse He Rode In On: The People V. Kenneth Starr NA James Carville 0.00 NA
15 OK .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 10.99 TOKYOPOP
16 OK 1 Ragged Ridge Road NA David Adams Richards 0.00 NA
17 OK 1,000 PLACES TO SEE BEFORE YOU DIE A guide for traveling the world; second edition updated with new entries. Patricia Schultz 0.00 Workman
18 OK 1,000 RECORDINGS TO HEAR BEFORE YOU DIE Operas, rock albums, blues, classical music, world music and other essentials, from a musician and music journalist. Tom Moon 0.00 Workman
19 OK 1,227 QUITE INTERESTING FACTS TO BLOW YOUR SOCKS OFF A compilation of interesting information on subjects ranging from Zeus to Zuckerberg. John Lloyd and others 0.00 Norton
20 OK 1,339 QUITE INTERESTING FACTS TO MAKE YOUR JAW DROP Informative morsels of arcana and triva. John Lloyd, John Mitchinson and James Harkin 0.00 Norton

Creating an aggregated data frame from the above to find the publishers popularity. And plot them in a graph

df1 <- sqldf("select publisher,count(*) Count from books_df group by publisher ") 
## Loading required package: tcltk
## Warning: Quoted identifiers should have class SQL, use DBI::SQL() if the
## caller performs the quoting.
 ggplot( data = df1, aes  (x = publisher, y=Count )) + 
   geom_bar(stat="identity") + 
   coord_flip() +
   scale_fill_manual(values=c("#ff0000", "#ffff00", "#00ff00")) + 
   xlab("Publisher") + 
   ylab("# of Boos Published") +
   ggtitle ("NY Times Best Book Publisher") +
   theme(legend.position="bottom") +
   guides(fill=guide_legend(title="Direction of Change Since Last Week:"))