Rathish Parayil Sasidharan

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

Prerequisite

Load the required libraries

library("XML")
library('jsonlite')
library('plyr')
library('tidyverse')

Convert XML to Dataframe

xmlData <-read_file('https://raw.githubusercontent.com/rathish-ps/Data607-Assignment/main/book7.xml')

## find the books(nodes)  under XPath /catalog/book
df1 <- ldply(xpathApply(xmlParse(xmlData), "/catalog/book", function(node) {
        
    #extract the data and build a data frame
        title <- xmlValue(node[["title"]])
        #find all authors
        author <- xpathSApply(node, "./authors/author", xmlValue)
        
        #to combine the authors into single row by comma seperated
        author<-paste(as.character(author), collapse=", ")
        
        genre <- xmlValue(node[["genre"]])
        year <- xmlValue(node[["publish_date"]])
        isbn <- xmlValue(node[["isbn"]])
        
        data.frame(title, author, genre, year, isbn, stringsAsFactors = FALSE)
}), data.frame)

df1
##           title                       author   genre year    isbn
## 1 War and Peace                  Leo,Tolstoy   Novel 1869 isbn100
## 2    Good Omens Neil,Gaiman, Terry,Pratchett Fantasy 1655 isbn101
## 3 The Alchemist                 Paulo,Coelho Fantasy 1988 isbn102

Convert JSON to Dataframe

book7json <-read_json("https://raw.githubusercontent.com/rathish-ps/Data607-Assignment/main/book7.json", simplifyVector = TRUE)
df2<-book7json$catalog$book
df2
##           title                       author   genre publish_date    isbn
## 1 War and Peace                  Leo,Tolstoy   Novel         1869 isbn100
## 2    Good Omens Neil Gaiman, Terry Pratchett Fantasy         1655 isbn101
## 3 The Alchemist                 Paulo,Coelho Fantasy         1988 isbn102

Convert HTML to Dataframe

book7htmlRaw <-read_file('https://raw.githubusercontent.com/rathish-ps/Data607-Assignment/main/book7.html')
book7html <-htmlParse(book7htmlRaw,encoding ="UTF-8")

html7Data <- readHTMLTable(book7html)
html7Df <- ldply(html7Data, data.frame)
html7Df
##    .id         Title                       Author   Genre Publish.Date    ISBN
## 1 NULL War and Peace                  Leo Tolstoy   Novel         1869 isbn100
## 2 NULL    Good Omens Neil Gaiman ,Terry Pratchett Fantasy         1655 isbn101
## 3 NULL The Alchemist                 Paulo Coelho Fantasy         1988 isbn102
df3<-select(html7Df,2:6)
df3
##           Title                       Author   Genre Publish.Date    ISBN
## 1 War and Peace                  Leo Tolstoy   Novel         1869 isbn100
## 2    Good Omens Neil Gaiman ,Terry Pratchett Fantasy         1655 isbn101
## 3 The Alchemist                 Paulo Coelho Fantasy         1988 isbn102

Summary

Loaded XML,JSON and HTML files with identical data into data frame and the values are identical