Here is the assignmnet work statment:
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].
library(XML)
library(RCurl) ## required for GetURL method
## Loading required package: bitops
library(jsonlite)
library(kableExtra)
library(knitr)
htmlParse is used to parse the html content and invoked readHTMLTable to extract the html table.
html.url <- "https://raw.githubusercontent.com/charlsjoseph/CUNY-Data607/master/week7/books.html"
html.url.content= getURL(html.url)
books.html <- htmlParse(file= html.url.content )
books.html.df <- as.data.frame(readHTMLTable(books.html))
books.html.df
## NULL.Book.Name NULL.Author
## 1 The Talisman Stephen King, Peter Straub
## 2 The Pearl John Steinbeck
## 3 POEM OF THE MAN-GOD Volume 1 Maria Valtorta
## NULL.Publisher NULL.Year.Published NULL.Pages
## 1 Pocket Books 1984 944
## 2 Penguin Books 1993 96
## 3 Centro Editoriale Valtortiano 2000 995
## NULL.Language
## 1 English
## 2 English
## 3 English
head(books.html.df)
## NULL.Book.Name NULL.Author
## 1 The Talisman Stephen King, Peter Straub
## 2 The Pearl John Steinbeck
## 3 POEM OF THE MAN-GOD Volume 1 Maria Valtorta
## NULL.Publisher NULL.Year.Published NULL.Pages
## 1 Pocket Books 1984 944
## 2 Penguin Books 1993 96
## 3 Centro Editoriale Valtortiano 2000 995
## NULL.Language
## 1 English
## 2 English
## 3 English
# print datafrane using kabble
books.html.df %>%
kable() %>%
kable_styling()
| NULL.Book.Name | NULL.Author | NULL.Publisher | NULL.Year.Published | NULL.Pages | NULL.Language |
|---|---|---|---|---|---|
| The Talisman | Stephen King, Peter Straub | Pocket Books | 1984 | 944 | English |
| The Pearl | John Steinbeck | Penguin Books | 1993 | 96 | English |
| POEM OF THE MAN-GOD Volume 1 | Maria Valtorta | Centro Editoriale Valtortiano | 2000 | 995 | English |
xmlParse is used to parse the xml content and invoked xmlToDataFrame to convert the content into dataframe
xml.url <- "https://raw.githubusercontent.com/charlsjoseph/CUNY-Data607/master/week7/books.xml"
xml.url.content= getURL(xml.url)
xml.url.content <- xmlParse(file=xml.url.content)
xml.url.df <- xmlToDataFrame(xml.url.content)
xml.url.df
## Book_Name Author
## 1 The Talisman Stephen King, Peter Straub
## 2 Pearl John Steinbeck
## 3 POEM OF THE MAN-GOD Volume 1 Maria Valtorta
## Publisher Year_Published Pages Language
## 1 Pocket Books 1984 944 English
## 2 Penguin Books 1993 96 English
## 3 Centro Editoriale Valtortiano 1990 995 English
# print datafrane using kabble
xml.url.df %>%
kable() %>%
kable_styling()
| Book_Name | Author | Publisher | Year_Published | Pages | Language |
|---|---|---|---|---|---|
| The Talisman | Stephen King, Peter Straub | Pocket Books | 1984 | 944 | English |
| Pearl | John Steinbeck | Penguin Books | 1993 | 96 | English |
| POEM OF THE MAN-GOD Volume 1 | Maria Valtorta | Centro Editoriale Valtortiano | 1990 | 995 | English |
fromJSON is used to parse and convert into the json df
json.url <- "https://raw.githubusercontent.com/charlsjoseph/CUNY-Data607/master/week7/books.json"
json.url.content= getURL(json.url)
json.url.df <- fromJSON(json.url.content)
json.url.df
## $books
## Book_Name Author
## 1 The Talisman Stephen King, Peter Straub
## 2 Pearl John Steinbeck
## 3 POEM OF THE MAN-GOD Volume 1 Maria Valtorta
## Publisher Year Pages Language
## 1 Pocket Books 1984 944 English
## 2 Penguin Books 1993 96 English
## 3 Centro Editoriale Valtortiano 1990 995 English
# print datafrane using kabble
json.url.df %>%
kable() %>%
kable_styling()
|
All three dataframes are almost same other than very minor difference in the column name. For e.g the column names in html df has a dot, but for xml and json has _. Also the column name is prefixed with a NULL.