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? 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].
Create tables in html, xml and json formate firstly, I had my design of table in excel file; secondly, I manually wrote the codes in notepade ++ and saved on my computer in html, xml and json formate; then, I uploaded my html, xml and json files in my github reposition. finally, I parsed all information by using r studio and read it.
library(jsonlite)
library(XML)
library(knitr)
library(RCurl)
## Loading required package: bitops
read html data from website
raw_HTML = getURL("https://raw.githubusercontent.com/johnpannyc/Jun-Pan-DATA-607-wk7-assignment/master/bestnovels.html", ssl.verifypeer = FALSE)
bestnovels_HTML = readHTMLTable(raw_HTML,
header = T,
which=1,
isHTML = TRUE,
stringsAsFactors = F)
view the data frame
kable(bestnovels_HTML)
Title | Author_1 | Author_2 | Year | Price | Language |
---|---|---|---|---|---|
Pilgrim’s Progress | John Bunyan | Null | 2018 | 4.99 | English |
Robinson Crusoe | Daniel Defoe | Null | 2017 | 7.99 | English |
Good Omens | Neil Gaiman | Terry Pratchett | 2011 | 7.63 | English |
read xml file from website
raw_XML = getURL("https://raw.githubusercontent.com/johnpannyc/Jun-Pan-DATA-607-wk7-assignment/master/bestnovels1.xml", ssl.verifypeer = FALSE)
bestnovels_XML = xmlParse(raw_XML)
bestnovels_child = xmlChildren(bestnovels_XML)
head(bestnovels_child)
## $books
## <books lang="en">
## <book>
## <Title>Pilgrim's Progress</Title>
## <Author_1>John Bunyan</Author_1>
## <Author_2>Null</Author_2>
## <Year>2018</Year>
## <Price>4.99</Price>
## <Language>English</Language>
## </book>
## <book>
## <Title>Robinson Crusoe</Title>
## <Author_1>Daniel Defoe</Author_1>
## <Author_2>Null</Author_2>
## <Year>2017</Year>
## <Price>7.99</Price>
## <Language>English</Language>
## </book>
## <book>
## <Title>Good Omens</Title>
## <Author_1>Neil Gaiman</Author_1>
## <Author_2>Terry Pratchett</Author_2>
## <Year>2011</Year>
## <Price>7.63</Price>
## <Language>English</Language>
## </book>
## </books>
create a dateframe
bestnovels_XML_df = data.frame()
bestnovels_XML_df = cbind(xpathSApply(bestnovels_XML, "//Title", xmlValue),
xpathSApply(bestnovels_XML, "//Author_1", xmlValue),
xpathSApply(bestnovels_XML, "//Author_2", xmlValue),
xpathSApply(bestnovels_XML, "//Year", xmlValue),
xpathSApply(bestnovels_XML, "//Price", xmlValue),
xpathSApply(bestnovels_XML, "//Language", xmlValue))
bestnovels_XML_df = data.frame(bestnovels_XML_df)
names(bestnovels_XML_df) = c("Title", "Author_1", "Author_2", "Year", "Price", "Language")
kable(bestnovels_XML_df)
Title | Author_1 | Author_2 | Year | Price | Language |
---|---|---|---|---|---|
Pilgrim’s Progress | John Bunyan | Null | 2018 | 4.99 | English |
Robinson Crusoe | Daniel Defoe | Null | 2017 | 7.99 | English |
Good Omens | Neil Gaiman | Terry Pratchett | 2011 | 7.63 | English |
read json file from website
raw_JSON = getURL("https://raw.githubusercontent.com/johnpannyc/Jun-Pan-DATA-607-wk7-assignment/master/bestnovles1.json", ssl.verifypeer = FALSE)
bestnovels_JSON = fromJSON(txt = raw_JSON)
str(bestnovels_JSON)
## List of 6
## $ Title : chr [1:3] "Pilgrim's Progress" "Robinson Crusoe" "Good Omens"
## $ Author_1: chr [1:3] "John Bunyan" "Daniel Defoe" "Neil Gaiman"
## $ Author_2: chr [1:3] "Null" "Null" "Terry Pratchett"
## $ Year : int [1:3] 2018 2017 2011
## $ Price : num [1:3] 4.99 7.99 7.63
## $ Language: chr [1:3] "English" "English" "English"
create a dataframe
bestnovels_JSON_df = as.data.frame(bestnovels_JSON)
kable(bestnovels_JSON_df)
Title | Author_1 | Author_2 | Year | Price | Language |
---|---|---|---|---|---|
Pilgrim’s Progress | John Bunyan | Null | 2018 | 4.99 | English |
Robinson Crusoe | Daniel Defoe | Null | 2017 | 7.99 | English |
Good Omens | Neil Gaiman | Terry Pratchett | 2011 | 7.63 | English |