library(RCurl)
library(XML)
library(jsonlite)
library(knitr)
library(kableExtra)

Assignment Overview

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”).

Write R code, using your packages of choice, to load the information from each of the three sources into separate R data frames. Are the three data frames identical?

Parse “books.html” Into A Data Frame

html_source <- getURL('https://raw.githubusercontent.com/stephen-haslett/data-607/master/week_seven_assignment/books.html')
books_html <- readHTMLTable(html_source)

# Convert the table to a data frame and remove the "NULL." values from the column titles.
books_html <- data.frame(books_html)
names(books_html) <- gsub("NULL.", "", names(books_html))
books_html %>% kable() %>% kable_styling(bootstrap_options = c('striped', 'bordered')) %>% row_spec(1:3, color = "black")
Title Authors Year.Published Publisher Genre Page.Count Description
1984 George Orwell 1949 Secker and Warburg Science Fiction 328 A dystopian novel by English novelist George Orwell.
A Clockwork Orange Anthony Burgess 1962 William Heinemann Dystopian Fiction 192 A dystopian satirical black comedy novel by English writer Anthony Burgess.
Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch Neil Gaiman, Terry Pratchett 1990 Workman Fantasy Fiction 288 A comedy about the birth of the son of Satan and the coming of the End Times.
is.data.frame(books_html)
## [1] TRUE

Parse “books.xml” Into A Data Frame

xml_source <- getURL('https://raw.githubusercontent.com/stephen-haslett/data-607/master/week_seven_assignment/books.xml')

xml_parsed <- xmlParse(xml_source)
books_xml <- xmlToDataFrame(xml_parsed)
books_xml %>% kable() %>% kable_styling(bootstrap_options = c('striped', 'bordered')) %>% row_spec(1:3, color = "black")
book_title authors year_published publisher genre page_count description
1984 Geogle Orwell 1949 Secker and Warburg Science Fiction 328 A dystopian novel by English novelist George Orwell.
A Clockwork Orange Anthony Burgess 1962 William Heinemann Dystopian Fiction 192 A dystopian satirical black comedy novel by English writer Anthony Burgess.
Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch Neil Gaiman, Terry Pratchett 1990 Workman Fantasy Fiction 288 A comedy about the birth of the son of Satan and the coming of the End Times.
is.data.frame(books_xml)
## [1] TRUE

Parse books.json Into A Data Frame

json_source <- getURL('https://raw.githubusercontent.com/stephen-haslett/data-607/master/week_seven_assignment/books.json')
books_json <- fromJSON(json_source)
books_json %>% kable() %>% kable_styling(bootstrap_options = c('striped', 'bordered')) %>% row_spec(1:3, color = "black")
Title Authors Year Published Publisher Genre Page Count Description
1984 George Orwell 1949 Secker & Warburg Science Fiction 328 A dystopian novel by English novelist George Orwell.
A Clockwork Orange Anthony Burgess 1962 William Heinemann Dystopian Fiction 192 A dystopian satirical black comedy novel by English writer Anthony Burgess.
Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch Neil Gaiman, Terry Pratchett 1990 Workman Fantasy Fiction 288 A comedy about the birth of the son of Satan and the coming of the End Times.
is.data.frame(books_json)
## [1] TRUE

Analysis: Are the three data frames identical?

The 3 data frames are almost identical with the exception of the column headers. The spaces between the words in the HTML dataframe column titles have been substituted with periods, and the XML dataframe uses the XML tag names as column titles. Additionally, the column widths in the JSON data frame differ from those in both the XML and HTML dataframes, which have identical column widths.