Assignment – Working with XML and JSON in R

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].

Loading an csv file

Regular csv for comparison

dfcsv <- read.csv("https://raw.githubusercontent.com/davidblumenstiel/CUNY-MSDS-Data-607/master/Homework%207/books.csv", stringsAsFactors = FALSE)
dfcsv
##                                                                    Title
## 1 The_Elements:_A_Visual_Exploration_of_Every_Known_Atom_in_the_Universe
## 2                     Superheavy:_Making_and_Breaking_the_Periodic_Table
## 3                               Oxygen:_The_Molecule_that_Made_the_World
##                    Authors Amazon_Price Date_Published
## 1 Theodore_Gray,_Nick_Mann        15.99       4/3/2012
## 2              Kit_Chapman        11.99      6/13/2019
## 3                Nick_Lane        12.62       7/1/2016

Loading an xml file

This uses the library RCurl to retreive the URL, and the library XML for an easy to-dataframe function

library("RCurl")
library("XML")

xmlurl <-getURL("https://raw.githubusercontent.com/davidblumenstiel/CUNY-MSDS-Data-607/master/Homework%207/books.xml")
dfxml <- xmlToDataFrame(xmlurl)
dfxml
##                                                                    Title
## 1 The_Elements:_A_Visual_Exploration_of_Every_Known_Atom_in_the_Universe
## 2                     Superheavy:_Making_and_Breaking_the_Periodic_Table
## 3                               Oxygen:_The_Molecule_that_Made_the_World
##                    Authors Amazon_Price Date_Published
## 1 Theodore_Gray,_Nick_Mann        15.99       4/3/2012
## 2              Kit_Chapman        11.99      6/13/2019
## 3                Nick_Lane        12.62       7/1/2016

Loading an html file

Needed a few libraies to read this into a dataframe simply. It comes out as a list of two, so only the first element (the data) is read into the dataframe

library("textreadr")
library("rvest")
## Loading required package: xml2
## 
## Attaching package: 'xml2'
## The following object is masked from 'package:textreadr':
## 
##     read_html
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:XML':
## 
##     xml
library("xml2")
dfhtml<-read_html('https://raw.githubusercontent.com/davidblumenstiel/CUNY-MSDS-Data-607/master/Homework%207/books.htm')
dfhtml<-html_table(dfhtml, dec = ".")[[1]]
dfhtml
##                                                                    Title
## 1 The_Elements:_A_Visual_Exploration_of_Every_Known_Atom_in_the_Universe
## 2                     Superheavy:_Making_and_Breaking_the_Periodic_Table
## 3                               Oxygen:_The_Molecule_that_Made_the_World
##                    Authors Amazon_Price Date_Published
## 1 Theodore_Gray,_Nick_Mann        15.99       4/3/2012
## 2              Kit_Chapman        11.99      6/13/2019
## 3                Nick_Lane        12.62       7/1/2016

Loading an json file

Used the jsonlite library for an easy import. Origionally, the file read as a long vector, but ‘simplifyVector’ reduced it

library(jsonlite)
dfjson <- read_json("https://raw.githubusercontent.com/davidblumenstiel/CUNY-MSDS-Data-607/master/Homework%207/books.json", simplifyVector = TRUE)
dfjson
##                                                                    Title
## 1 The_Elements:_A_Visual_Exploration_of_Every_Known_Atom_in_the_Universe
## 2                     Superheavy:_Making_and_Breaking_the_Periodic_Table
## 3                               Oxygen:_The_Molecule_that_Made_the_World
##                    Authors Amazon_Price Date_Published
## 1 Theodore_Gray,_Nick_Mann        15.99       4/3/2012
## 2              Kit_Chapman        11.99      6/13/2019
## 3                Nick_Lane        12.62       7/1/2016

All of these are pretty much identical. The only difference I can spot is that the data types in the xml file all came across as factors.