R Markdown Week 7 Assignment

Week 7 Assignment

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?

#Load required Libraries
library(XML)
library(R2HTML)
library(jsonlite)
library(RCurl)
## Loading required package: bitops
library(rvest)
## Loading required package: xml2
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:XML':
## 
##     xml
library(DT)

3 sources of data HTML, XML and JSON are available in Github.

Read HTML file from Git Hub which contains 4 records for the books information. Create dataframe and datatable to compare

#Read HTML file from Git Hub URl : https://raw.githubusercontent.com/vijay564/R-Maincode/master/books.html
books.html <- read_html("https://raw.githubusercontent.com/vijay564/R-Maincode/master/books.html", encoding = "UTF-8")
books.tables <- html_nodes(books.html, "table")
books.html <- html_table(books.tables, header = TRUE)
books.html
## [[1]]
##                                                                    Title
## 1                                                             Advanced R
## 2                Data Science from scratch: First Principals with Python
## 3 R for Data Science: Import, Tidy, Transform, Visualize, and Model Data
## 4                                                    R Graphics Cookbook
##                           Author(s)           ISBN Publishing Year
## 1                    Hadley Wickham 978-1466586963            2015
## 2                         Joel Grus 978-1491901427            2015
## 3 Hadley Wickham, Garrett Grolemund 978-1491910399            2016
## 4                     Winston Chang 978-1449316952            2013
#leaning the data
html_table_clean <- books.html[[1]]
html.df <- as.data.frame(html_table_clean)
#Create Datatable
datatable(html.df)

Read XML file from Git Hub which contains 4 records for the books information. Create dataframe and datatable to compare

#Read XML for Git Hib URl : https://raw.githubusercontent.com/vijay564/R-Maincode/master/books.xml
books.url = getURL("https://raw.githubusercontent.com/vijay564/R-Maincode/master/books.xml")
books.parse <- xmlParse(books.url)
# Parsing
books.root <- xmlRoot(books.parse)
books.xml <- xmlToDataFrame(books.root, stringsAsFactors = FALSE)
# Creating datatable
datatable(books.xml)

Read JSON file from Git Hub which contains 4 records for the books information. Create dataframe and datatable to compare

#Read JSON file from Git Hub URl : https://raw.githubusercontent.com/vijay564/R-Maincode/master/books.json
json.url <- getURL("https://raw.githubusercontent.com/vijay564/R-Maincode/master/books.json")
json.books <- fromJSON(json.url)
json.books
## $data_books
##                                                                    title
## 1                                                             Advanced R
## 2                Data Science from scratch: First Principals with Python
## 3 R for Data Science: Import, Tidy, Transform, Visualize, and Model Data
## 4                                                    R Graphics Cookbook
##                             authors              ISBN publishing_year
## 1                    Hadley Wickham    978-1466586963            2015
## 2                         Joel Grus    978-1491901427            2015
## 3 Hadley Wickham, Garrett Grolemund    978-1491910399            2016
## 4                     Winston Chang 978-1-449-31695-2            2013
json.books.df <- data.frame(json.books$`data_books`)
#Create Datatable
datatable(json.books.df)

Compare if the 3 dataframes created from HTML, XML and JSON are identical

# Check if the dataframes are identical or not
identical(html.df, books.xml)
## [1] FALSE
identical(html.df, json.books.df)
## [1] FALSE
identical(books.xml, json.books.df)
## [1] FALSE

Conclusion :

After comparing HTMl, XML and JSON dataframes, we found they are similar but not identical eventhough the datatable content remains same across all the 3 files.

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.