QUESTION:
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.
DIRECTION:
Write R code, using your packages of choice, to load the information from each of the three sources into separate R data frames.
XML Format
# Please kindly install these packages if you dont have it yet, and load it.
library(XML)
library(RCurl)
## Loading required package: bitops
library(knitr)
library(htmltab)
## Warning: package 'htmltab' was built under R version 3.2.4
library(jsonlite)
# Getting xml data loaded from URL
url = "https://raw.githubusercontent.com/mascotinme/MSDA-IS607/f896255f3b817cad1496bb767922bb7c72457422/book.xml"
data = getURL(url, ssl.verifypeer=0L, followlocation=1L)
book1 <- xmlParse(data)
book <- xmlToDataFrame(book1)
kable(head(book))
| name | isbn | author | year | pages | genre | type |
|---|---|---|---|---|---|---|
| Engineering mathematics: 7th Edition | 978-1100005 | Dexter Booth, Ken Stroud | 2013 | 1057 | Educational | Hard Cover |
| American Sniper: The Autobiography of the Most Lethal Sniper in U.S. Military History | 978-0062238863 | Chris Kyle, Scott McEwen, Jim Defelice | 2013 | 448 | Literary Literature | PaperBack |
| the catcher in the rye | 978-0316769488 | j.d salinger | 1951 | 240 | coming of age | PaperBack |
HTML FORMAT
# load HTML data from URL and ingnore the warning statement(s).
options(warn = -1)
url = "https://raw.githubusercontent.com/mascotinme/MSDA-IS607/master/Books1.html"
books_html = htmltab(doc = url, which = 1)
books_html = data.frame(books_html, row.names = NULL)
kable(books_html)
| Name | ISBN | Author | Year | Pages | Genre | Type |
|---|---|---|---|---|---|---|
| Engineering mathematics: 7th Edition | 978-1100000000 | Dexter Booth, Ken Stroud | 2013 | 1057 | Educational | Hard Cover |
| American Sniper: The Autobiography of the Most Lethal Sniper in U.S. Military History | 978-0062238863 | Chris Kyle, Scott McEwen, Jim Defelice | 2013 | 448 | Literary literature | PackBack |
| The Catcher In The Rye | 978-0316769488 | J.D Salinger | 1951 | 240 | Coming of Age | PackBack |
JSON FORMAT
#loading json file from URL
url2 = "https://raw.githubusercontent.com/mascotinme/MSDA-IS607/master/Books.json"
books_json = getURL(url2)
books_json = fromJSON(books_json)
kable(books_json)
|
Question: Are the three data frames identical?
YES!, the three data frames are identical.
REFERENCES:
http://www.rprimer.dk/index.php?page=showrule&index=2
https://cran.r-project.org/web/packages/jsonlite/vignettes/json-aaquickstart.html