INTRODUCTION
Assignment: Pick three of your favorite books on one of your favorite subjects. At least one of the books should have more than one author. For each book, include the title, authors, and two or three other attributes that you find interesting. Take the information that you’ve selected about these three books, and separately create three files which store the book’s information in HTML (using an html table), XML, and JSON formats (e.g. “books.html”, “books.xml”, and “books.json”). To help you better understand the different file structures, I’d prefer that you create each of these files “by hand” unless you’re already very comfortable with the file formats.
I chose three books on business that I’ve read this year and included the title, author(s), ISBN, publisher, date published, and number of pages.
PREPARE
Load required libraries.
library(XML)
library(R2HTML)
library(jsonlite)
library(RCurl)
## Loading required package: bitops
library(DT)
XML
Read in the table from the XML file.
xml_url <- getURL("https://raw.githubusercontent.com/jillenergy/Week-7-Books/master/Books-JCA-xml.xml")
xml_books <- xmlParse(xml_url)
xml_books
## <?xml version="1.0" encoding="UTF-8"?>
## <Business_Books_Read_in_2017>
## <Book>
## <Title>The Power of Habit- Why We Do What We Do in Life and Business</Title>
## <Author>Charles Duhigg</Author>
## <Author/>
## <ISBN>978-0812981605</ISBN>
## <Publisher>Random House Trade Paperbacks</Publisher>
## <Published>01/07/2014</Published>
## <Pages>372</Pages>
## </Book>
## <Book>
## <Title>Radical Candor- Be a Kick-Ass Boss Without Losing Your Humanity</Title>
## <Author>Kim Scott</Author>
## <Author/>
## <ISBN>978-1250103505</ISBN>
## <Publisher>St. Martin's Press</Publisher>
## <Published>03/14/2017</Published>
## <Pages>272</Pages>
## </Book>
## <Book>
## <Title>Switch- How to Change Things When Change Is Hard</Title>
## <Author>Chip Heath</Author>
## <Author>Dan Heath</Author>
## <ISBN>978-0385528757</ISBN>
## <Publisher>Crown Business</Publisher>
## <Published>02/16/2010</Published>
## <Pages>305</Pages>
## </Book>
## </Business_Books_Read_in_2017>
##
Convert XML to a dataframe.
xml_new_df <- data.frame(t(xmlSApply(xml_new, function(x) xmlSApply(x, xmlValue))), row.names = NULL)
datatable(xml_new_df)
HTML
Read in table from the HTML file.
html_url <- getURL("https://raw.githubusercontent.com/jillenergy/Week-7-Books/master/Books-JCA-html.html")
html_books <- readHTMLTable(html_url, header = TRUE)
html_books
## $`Business Books Read in 2017`
## Title
## 1 The Power of Habit- Why We Do What We Do in Life and Business
## 2 Radical Candor- Be a Kick-Ass Boss Without Losing Your Humanity
## 3 Switch- How to Change Things When Change Is Hard
## Author Author ISBN Publisher
## 1 Charles Duhigg 978-0812981605 Random House Trade Paperbacks
## 2 Kim Scott 978-1250103505 St. Martin's Press
## 3 Chip Heath Dan Heath 978-0385528757 Crown Business
## Date Published Pages
## 1 01/07/2014 372
## 2 03/14/2017 272
## 3 02/16/2010 305
Convert HTML to a dataframe.
html_books_df <- as.data.frame(html_books)
colnames(html_books_df) <- c("Title", "Author", "Author", "ISBN", "Publisher", "Published", "Pages")
datatable(html_books_df)
JSON
Read in table from JSON file.
json_url <- getURL("https://raw.githubusercontent.com/jillenergy/Week-7-Books/master/Books-JCA-json.json")
json_books <- fromJSON(json_url)
json_books
## $`Business Books Read in 2017`
## Title
## 1 The Power of Habit- Why We Do What We Do in Life and Business
## 2 Radical Candor- Be a Kick-Ass Boss Without Losing Your Humanity
## 3 Switch- How to Change Things When Change Is Hard
## Author ISBN Publisher
## 1 Charles Duhigg 978-0812981605 Random House Trade Paperbacks
## 2 Kim Scott 978-1250103505 St. Martin's Press
## 3 Chip Heath, Dan Heath 978-0385528757 Crown Business
## Published Pages
## 1 01/07/2014 372
## 2 03/14/2017 272
## 3 02/16/2010 305
Convert JSON to a dataframe.
json_books_df <- data.frame(json_books$`Business Books Read in 2017`)
datatable(json_books_df)
ANALYZE
The three dataframes are similar but not identical. The JSON dataframe combines the authors into one column.