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?
Your deliverable is the three source files and the R code. If you can, package your assignment solution up into an .Rmd file and publish to rpubs.com. [This will also require finding a way to make your three text files accessible from the web].
library(XML)
library(RCurl)
library(jsonlite)
library(DT)
htmlURL <- "https://raw.githubusercontent.com/betsyrosalen/DATA_607_Data_Acquisition_and_Management/master/Assignment7/books.html"
htmlContent <- getURLContent(htmlURL)
booksHTML <- readHTMLTable(htmlContent)
booksHTML <- booksHTML[[1]]
datatable(booksHTML)
str(booksHTML)
## 'data.frame': 4 obs. of 6 variables:
## $ Title : Factor w/ 4 levels "Breaking the Maya Code",..: 4 2 1 3
## $ Author : Factor w/ 4 levels "Lee Hendrix, Thea Vignau-Wilberg",..: 4 1 2 3
## $ ISBN-13 : Factor w/ 4 levels "978-0500285534",..: 4 3 2 1
## $ Pages : Factor w/ 4 levels "176","296","304",..: 2 4 3 1
## $ Binding : Factor w/ 3 levels "Flexibound","Hardcover",..: 1 2 3 3
## $ AmazonURL: Factor w/ 4 levels "http://a.co/0KWAXs7",..: 1 3 4 2
xmlURL <- "https://raw.githubusercontent.com/betsyrosalen/DATA_607_Data_Acquisition_and_Management/master/Assignment7/books.xml"
xmlContent <- getURLContent(xmlURL)
booksXMLparsed <- xmlParse(xmlContent)
booksXML <- xmlToDataFrame(booksXMLparsed)
datatable(booksXML)
str(booksXML)
## 'data.frame': 4 obs. of 6 variables:
## $ title : Factor w/ 4 levels "Breaking the Maya Code",..: 4 2 1 3
## $ author : Factor w/ 4 levels "Lee Hendrix, Thea Vignau-Wilberg",..: 4 1 2 3
## $ ISBN-13 : Factor w/ 4 levels "978-0500285534",..: 4 3 2 1
## $ pages : Factor w/ 4 levels "176","296","304",..: 2 4 3 1
## $ binding : Factor w/ 3 levels "Flexibound","Hardcover",..: 1 2 3 3
## $ amazonURL: Factor w/ 4 levels "http://a.co/0KWAXs7",..: 1 3 4 2
jsonURL <- "https://raw.githubusercontent.com/betsyrosalen/DATA_607_Data_Acquisition_and_Management/master/Assignment7/books.json"
booksJSON <- fromJSON(jsonURL)
booksJSON <- booksJSON[[1]]
datatable(booksJSON)
str(booksJSON)
## 'data.frame': 4 obs. of 6 variables:
## $ title : chr "The Design of Dissent, Expanded Edition: Greed, Nationalism, Alternative Facts, and the Resistance" "Mira calligraphiae monumenta: A Sixteenth-century Calligraphic Manuscript inscribed by Georg Bocskay and Illumi"| __truncated__ "Breaking the Maya Code" "Reading the Maya Glyphs"
## $ author :List of 4
## ..$ : chr "Milton Glaser" "Mirko Ilic" "Steven Heller" "Tony Kushner"
## ..$ : chr "Lee Hendrix" "Thea Vignau-Wilberg"
## ..$ : chr "Michael D. Coe"
## ..$ : chr "Michael D. Coe" "Mark Van Stone"
## $ ISBN-13 : chr "978-1631594243" "978-0892362127" "978-0500289556" "978-0500285534"
## $ pages : int 296 424 304 176
## $ binding : chr "Flexibound" "Hardcover" "Paperback" "Paperback"
## $ amazonURL: chr "http://a.co/0KWAXs7" "http://a.co/dSN7nG6" "http://a.co/esWdAA8" "http://a.co/0O6t7t9"
The XML and HTML data frames are identical. All variables in the data frame are factors with the same number of levels. The JSON file however created a data frame with variables of more logical datatypes based on the type of data in each one. Title, ISBN-13, binding and amazonURL are character vectors while pages is an integer vector and author is a list of 4 character vectors.