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.
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?
I began by picking three books on the subject of mathematics. For each book, I include the Title, Author, ISBN#, Publisher, date of publication, and number of pages.
library(RCurl)
## Loading required package: bitops
library(XML)
library(jsonlite)
library(DT)
# https://stackoverflow.com/questions/1395528/scraping-html-tables-into-r-data-frames-using-the-xml-package/1401367#1401367
# Load the url
bookHtml <- getURL("https://raw.githubusercontent.com/JoshuaSturm/CUNY_MSDA/Public/Fall_2017/DATA_607/DATA_607_Homework_7/DATA_607_Homework_7_HTML.html")
# htmlTreeParse will get the html format, and the parameters will ignore errors
bookHtml <- htmlTreeParse(bookHtml, error=function(...){}, useInternalNodes = TRUE)
# From the parsed html, extract the header row first, and then the others
header <- xpathSApply(bookHtml, "//table/tr/th", xmlValue)
values <- xpathSApply(bookHtml, "//table/tr/td", xmlValue)
# Convert to dataframe, and set the header
bookHtml <- as.data.frame(matrix(values, ncol = 6, byrow = T))
names(bookHtml) <- header
# Formatted nicely using the DT package
datatable(bookHtml)
# Load the url
bookXml <- getURL("https://raw.githubusercontent.com/JoshuaSturm/CUNY_MSDA/Public/Fall_2017/DATA_607/DATA_607_Homework_7/DATA_607_Homework_7_XML.xml")
# Convert the xml data to a dataframe, and display using the DT package
bookXml <- xmlToDataFrame(bookXml)
datatable(bookXml)
# Load the url
bookJson <- getURL("https://raw.githubusercontent.com/JoshuaSturm/CUNY_MSDA/Public/Fall_2017/DATA_607/DATA_607_Homework_7/DATA_607_Homework_7_JSON.json")
bookJson <- fromJSON(bookJson)
bookJson <- as.data.frame(bookJson)
datatable(bookJson)
JSON seems to be the most efficient: it’s the smallest file, and simple to parse. The only difference I noticed was the HTML parser had difficulty handling foreign characters, as can be seen in one of the author’s names. Otherwise, the output of all three seems pretty similar.