Assignment overview

This is a warm up exercise to help you to get more familiar with the HTML, XML, and JSON file formats, and using packages to read these data formats for downstream use in R data frames. In the next two class weeks, we’ll be loading these file formats from the web, using web scraping and web APIs.

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

Load required package

I used the XML package to parse the XML and HTML files, and the jsonlite package to parse the JSON file.

Extracting from XML

I used different IDE to write the XML file - because R studio doesn’t support the xml markup language. For this assignment, I used Visual studio code IDE from Microsoft. Then, I pushed the .xml, .json, and .html file to my GitHub account and used the getURL() to pull down the data into R

## [1] "DataScience_Books"
## [1] 3

Now, I got the DataScience_Books tag as a root element and have three nested elements as children nodes inside of it. To get each child node name, I have to loop over the nodes and get the content as a string, I used xmlSApply to the root and also as a nested call to pull out the children values using xmlValue() function.

## [1] "matrix"

The output is a matrix, we need to convert it to a dataframe using as.data.frame()

## [1] "data.frame"

Extracting from json

## [1] "list"

As results show, the Author columns has a nested array children books with multiple authors. To solve this issue, we can use tidyr to mutate that column to separate in the ,

## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 2 rows [1,
## 3].

Now, I got the DataScience_Books tag as a root element and have three nested elements as children nodes inside of it. To get each child node name, I have to loop over the nodes and get the content as a string, I used xmlSApply to the root and also as a nested call to pull out the children values using xmlValue() function.

## [1] "matrix"

The output is a matrix, we need to convert it to a dataframe using as.data.frame()

## [1] "data.frame"

Extraction from html

## $`Datascience Books`
##                                                        Title
## 1 Hands-On Machine Learning with Scikit-Learn and TensorFlow
## 2     Foundations of Statistical Natural Language Processing
## 3                                        R Graphics Cookbook
##                   Author           Author Year Published
## 1         Aurelien Geron                            2017
## 2 Christopher D. Manning Hinrich Schiitze           2000
## 3          Winston Cheng                            2012
##                                                Publisher Pages
## 1                                               O'REILLY   564
## 2 The MIT Press Cambridge, Massachusetts London, England   704
## 3                                               O'REILLY   413
##                ISBN
## 1 978-1-491-69229-9
## 2    978-0262133609
## 3 978-1-449-31695-2
## [1] "list"