Assignment - Web APIs
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.
Load Libraries:
library(httr)
library(rjson)
library(jsonlite)
##
## Attaching package: 'jsonlite'
## The following objects are masked from 'package:rjson':
##
## fromJSON, toJSON
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Load Books API:
books <- GET("https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json",
query = list(api_key = "8a79e79ab80a4363b99376b942f5c0a7"))
names(books)
## [1] "url" "status_code" "headers" "all_headers" "cookies"
## [6] "content" "date" "times" "request" "handle"
books$status_code
## [1] 200
Transform into Data Frame:
booksdf <- fromJSON(books$url) %>% as.data.frame()
booksdf
## status
## 1 OK
## 2 OK
## 3 OK
## 4 OK
## 5 OK
## 6 OK
## 7 OK
## 8 OK
## 9 OK
## 10 OK
## 11 OK
## 12 OK
## 13 OK
## 14 OK
## 15 OK
## 16 OK
## 17 OK
## 18 OK
## 19 OK
## 20 OK
## copyright
## 1 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 2 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 3 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 4 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 5 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 6 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 7 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 8 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 9 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 10 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 11 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 12 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 13 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 14 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 15 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 16 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 17 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 18 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 19 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## 20 Copyright (c) 2018 The New York Times Company. All Rights Reserved.
## num_results
## 1 31192
## 2 31192
## 3 31192
## 4 31192
## 5 31192
## 6 31192
## 7 31192
## 8 31192
## 9 31192
## 10 31192
## 11 31192
## 12 31192
## 13 31192
## 14 31192
## 15 31192
## 16 31192
## 17 31192
## 18 31192
## 19 31192
## 20 31192
## results.title
## 1 "I GIVE YOU MY BODY ..."
## 2 "MOST BLESSED OF THE PATRIARCHS"
## 3 #ASKGARYVEE
## 4 #GIRLBOSS
## 5 $100 STARTUP
## 6 $20 PER GALLON
## 7 '57, Chicago
## 8 'ROCK OF AGES: ''ROLLING STONE'' HISTORY OF ROCK AND ROLL'
## 9 'THE HIGH ROAD TO CHINA: GEORGE BOGLE, THE PANCHEN LAMA AND THE FIRST BRITISH EXPEDITION TO TIBET'
## 10 'TIL DEATH
## 11 'TIL DEATH DO US PART
## 12 'Til Faith Do Us Part: How Interfaith Marriage is Transforming America
## 13 'TIS THE SEASON
## 14 ------, THAT'S DELICIOUS
## 15 ...and the Horse He Rode In On: The People V. Kenneth Starr
## 16 .HACK G.U. , VOL. 5
## 17 1 Ragged Ridge Road
## 18 1,000 PLACES TO SEE BEFORE YOU DIE
## 19 1,000 RECORDINGS TO HEAR BEFORE YOU DIE
## 20 1,227 QUITE INTERESTING FACTS TO BLOW YOUR SOCKS OFF
## results.description
## 1 The author of the Outlander novels gives tips on writing sex scenes, drawing on examples from the books.
## 2 A character study that attempts to make sense of Jeffersons contradictions.
## 3 The entrepreneur expands on subjects addressed on his Internet show, like marketing, management and social media.
## 4 An online fashion retailer traces her path to success.
## 5 How to build a profitable start up for $100 or less and be your own boss.
## 6
## 7 <NA>
## 8 <NA>
## 9 <NA>
## 10
## 11 A matchmaker in Victorian England turns to a crime novelist for help when she starts receiving a series of disturbingly personalized trinkets.
## 12 <NA>
## 13 Two classic holiday stories "Under the Christmas Tree" (2009) and "Midnight Confessions" (2010) plus the novella "Backward Glance" (1991).
## 14
## 15 <NA>
## 16 This series, set in the future, is about an online, multiplayer game run amok. This volume concludes the tale.
## 17 <NA>
## 18 A guide for traveling the world; second edition updated with new entries.
## 19 Operas, rock albums, blues, classical music, world music and other essentials, from a musician and music journalist.
## 20 A compilation of interesting information on subjects ranging from Zeus to Zuckerberg.
## results.contributor
## 1 by Diana Gabaldon
## 2 by Annette Gordon-Reed and Peter S. Onuf
## 3 by Gary Vaynerchuk
## 4 by Sophia Amoruso
## 5 by Chris Guillebeau
## 6 by Christopher Steiner
## 7 <NA>
## 8 <NA>
## 9 <NA>
## 10 by Sharon Sala
## 11 by Amanda Quick
## 12 <NA>
## 13 by Robyn Carr
## 14 by Action Bronson with Rachel Wharton
## 15 <NA>
## 16 by Hamazaki Tatsuya
## 17 <NA>
## 18 by Patricia Schultz
## 19 by Tom Moon
## 20 by John Lloyd and others
## results.author results.contributor_note
## 1 Diana Gabaldon
## 2 Annette Gordon-Reed and Peter S Onuf
## 3 Gary Vaynerchuk
## 4 Sophia Amoruso
## 5 Chris Guillebeau
## 6 Christopher Steiner
## 7 Steve Monroe <NA>
## 8 GEOFFREY STOKES, KEN TUCKER' 'ED WARD <NA>
## 9 KATE TELTSCHER <NA>
## 10 Sharon Sala
## 11 Amanda Quick
## 12 Naomi Schaefer Riley <NA>
## 13 Ron Carr
## 14 Action Bronson with Rachel Wharton
## 15 James Carville <NA>
## 16 Hamazaki Tatsuya
## 17 David Adams Richards <NA>
## 18 Patricia Schultz
## 19 Tom Moon
## 20 John Lloyd and others
## results.price results.age_group results.publisher
## 1 0.00 Dell
## 2 0.00 Liveright
## 3 0.00 HarperCollins
## 4 0.00 Portfolio/Penguin/Putnam
## 5 23.00 Crown Business
## 6 0.00 Grand Central
## 7 0.00 <NA> <NA>
## 8 0.00 <NA> <NA>
## 9 0.00 <NA> <NA>
## 10 0.00 Harlequin Mira
## 11 0.00 Berkley
## 12 0.00 <NA> <NA>
## 13 0.00 Harlequin Mira
## 14 0.00 Abrams
## 15 0.00 <NA> <NA>
## 16 10.99 TOKYOPOP
## 17 0.00 <NA> <NA>
## 18 0.00 Workman
## 19 0.00 Workman
## 20 0.00 Norton
## results.isbns
## 1 0399178570, 9780399178573
## 2 0871404427, 9780871404428
## 3 0062273124, 0062273132, 9780062273123, 9780062273130
## 4 039916927X, 1591847931, 9780399169274, 9781591847939
## 5 0307951529, 9780307951526
## 6 NULL
## 7 0786867302, 9780786867301
## 8 0671630687, 9780671630683
## 9 0374217009, 9780374217006
## 10 0778314278, 9780778314271
## 11 069819361X, 039917446X, 9780698193611, 9780399174469
## 12 0199873747, 9780199873746
## 13 0778316645, 9780778316640
## 14 1419726552, 9781419726552
## 15 0684857340, 9780684857343
## 16 NULL
## 17 0671003542, 9780671003548
## 18 0761156860, 0761104844, 9780761156864, 9780761104841
## 19 NULL
## 20 0393241033, 9780393241037
## results.ranks_history
## 1 0399178570, 9780399178573, 8, Advice How-To and Miscellaneous, Advice, How-To & Miscellaneous, 2016-09-04, 2016-08-20, 1, NA, 0, 0
## 2 0871404427, 9780871404428, 16, Hardcover Nonfiction, Hardcover Nonfiction, 2016-05-01, 2016-04-16, 1, NA, 1, 0
## 3 0062273124, 0062273124, 9780062273123, 9780062273123, 5, 6, Business Books, Advice How-To and Miscellaneous, Business, Advice, How-To & Miscellaneous, 2016-04-10, 2016-03-27, 2016-03-26, 2016-03-12, 0, 1, NA, NA, 0, 0, 1, 1
## 4 1591847931, 1591847931, 1591847931, 1591847931, 039916927X, 039916927X, 039916927X, 039916927X, 9781591847939, 9781591847939, 9781591847939, 9781591847939, 9780399169274, 9780399169274, 9780399169274, 9780399169274, 8, 9, 9, 8, 10, 8, 14, 13, Business Books, Business Books, Business Books, Business Books, Business Books, Business Books, Advice How-To and Miscellaneous, Advice How-To and Miscellaneous, Business, Business, Business, Business, Business, Business, Advice, How-To & Miscellaneous, Advice, How-To & Miscellaneous, 2016-03-13, 2016-01-17, 2015-12-13, 2015-11-15, 2014-11-09, 2014-10-12, 2014-09-21, 2014-09-14, 2016-02-27, 2016-01-02, 2015-11-28, 2015-10-31, 2014-10-25, 2014-09-27, 2014-09-06, 2014-08-30, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## 5 NULL
## 6 NULL
## 7 NULL
## 8 NULL
## 9 NULL
## 10 NULL
## 11 069819361X, 069819361X, 9780698193611, 9780698193611, 15, 9, Combined Print and E-Book Fiction, E-Book Fiction, Combined Print & E-Book Fiction, E-Book Fiction, 2016-05-08, 2016-05-08, 2016-04-23, 2016-04-23, 1, 1, NA, NA, 0, 0, 0, 0
## 12 NULL
## 13 0778316645, 0778316645, 9780778316640, 9780778316640, 18, 6, Mass Market Paperback, Mass Market Paperback, Paperback Mass-Market Fiction, Paperback Mass-Market Fiction, 2014-11-30, 2014-11-16, 2014-11-15, 2014-11-01, 0, 1, NA, NA, 0, 1, 0, 0
## 14 1419726552, 9781419726552, 9, Advice How-To and Miscellaneous, Advice, How-To & Miscellaneous, 2017-10-01, 2017-09-16, 1, NA, 0, 1
## 15 NULL
## 16 NULL
## 17 NULL
## 18 0761156860, 0761156860, 0761156860, 9780761156864, 9780761156864, 9780761156864, 10, 8, 12, Travel, Travel, Travel, Travel, Travel, Travel, 2015-04-12, 2015-03-15, 2015-01-11, 2015-03-28, 2015-02-28, 2014-12-27, 0, 0, 0, NA, NA, NA, 0, 0, 0, 0, 0, 0
## 19 NULL
## 20 NULL
## results.reviews
## 1 , , ,
## 2 , , ,
## 3 , , ,
## 4 , , ,
## 5 , , ,
## 6 , , ,
## 7 , NA, https://www.nytimes.com/2001/07/29/books/books-in-brief-fiction-poetry-319660.html, NA
## 8 , NA, https://www.nytimes.com/1986/12/28/books/three-chord-music-in-a-three-piece-suit.html, NA
## 9 , NA, https://www.nytimes.com/2007/04/22/books/review/Stuart.t.html, NA
## 10 , , ,
## 11 , , ,
## 12 , NA, https://www.nytimes.com/2013/05/26/books/review/til-faith-do-us-part-by-naomi-schaefer-riley.html, NA
## 13 , , ,
## 14 , , ,
## 15 , NA, https://www.nytimes.com/1998/10/18/books/preaching-to-the-converted.html, NA
## 16 , , ,
## 17 , NA, https://www.nytimes.com/1997/08/31/books/books-in-brief-fiction-747866.html, NA
## 18 , , ,
## 19 , , ,
## 20 , , ,