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. Answer: My 3 favourite books are 1. Becoming , 2. Harry Potter and the Goblet of Fire: The Illustrated Edition, 3. Dash & Lily’s Book of Dares. I’ve made 3 files books.html, books.xml and books.json with the information of title, author, rating, url and year published.
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? Answer:
library("rvest")
## Warning: package 'rvest' was built under R version 3.5.3
## Loading required package: xml2
rawHTML <- paste(readLines("books.html"), collapse="\n")
html <- read_html(rawHTML)
data <- html_table(html)
data
## [[1]]
## Title
## 1 Becoming
## 2 Dash & Lily's Book of Dares
## 3 Harry Potter and the Goblet of Fire: The Illustrated Edition
## Authors Rating Published Year
## 1 Michelle Obama 4.9 2018
## 2 David Levithan,Rachel Cohn 4.1 2010
## 3 J. K. Rowling,Jim Kay NA 2019
## url
## 1 https://www.amazon.com/Becoming-Michelle-Obama/dp/1524763136/ref=zg_bs_books_7/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ
## 2 https://www.amazon.com/gp/product/0375866590/ref=x_gr_e_friend_sout_bb?ie=UTF8&tag=x_gr_e_friend_sout_bb-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0375866590&SubscriptionId=1MGPYB6YW3HWK55XCGG2
## 3 https://www.amazon.com/Harry-Potter-Goblet-Fire-Illustrated/dp/0545791421/ref=zg_bs_books_19/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ
Get the html table
html_table_data <- data[[1]]
html_table_data
## Title
## 1 Becoming
## 2 Dash & Lily's Book of Dares
## 3 Harry Potter and the Goblet of Fire: The Illustrated Edition
## Authors Rating Published Year
## 1 Michelle Obama 4.9 2018
## 2 David Levithan,Rachel Cohn 4.1 2010
## 3 J. K. Rowling,Jim Kay NA 2019
## url
## 1 https://www.amazon.com/Becoming-Michelle-Obama/dp/1524763136/ref=zg_bs_books_7/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ
## 2 https://www.amazon.com/gp/product/0375866590/ref=x_gr_e_friend_sout_bb?ie=UTF8&tag=x_gr_e_friend_sout_bb-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0375866590&SubscriptionId=1MGPYB6YW3HWK55XCGG2
## 3 https://www.amazon.com/Harry-Potter-Goblet-Fire-Illustrated/dp/0545791421/ref=zg_bs_books_19/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ
Read the json data
library(jsonlite)
json_table_df <- fromJSON("books.json")
json_table_df
## Title
## 1 Becoming
## 2 Dash & Lily's Book of Dares
## 3 Harry Potter and the Goblet of Fire: The Illustrated Edition
## Authors Rating Published Year
## 1 Michelle Obama 4.9 2018
## 2 David Levithan,Rachel Cohn 4.1 2010
## 3 J. K. Rowling,Jim Kay NA 2019
## url
## 1 https://www.amazon.com/Becoming-Michelle-Obama/dp/1524763136/ref=zg_bs_books_7/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ
## 2 https://www.amazon.com/gp/product/0375866590/ref=x_gr_e_friend_sout_bb?ie=UTF8&tag=x_gr_e_friend_sout_bb-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0375866590&SubscriptionId=1MGPYB6YW3HWK55XCGG2
## 3 https://www.amazon.com/Harry-Potter-Goblet-Fire-Illustrated/dp/0545791421/ref=zg_bs_books_19/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ
Read the books.xml file
library(XML)
##
## Attaching package: 'XML'
## The following object is masked from 'package:rvest':
##
## xml
xml_book <- xmlParse("books.xml")
rootNode <- xmlRoot(xml_book)
rootNode[1]
## $row
## <row>
## <Title>Becoming</Title>
## <Authors>Michelle Obama</Authors>
## <Rating>4.9</Rating>
## <Published_Year>2018</Published_Year>
## <url>https://www.amazon.com/Becoming-Michelle-Obama/dp/1524763136/ref=zg_bs_books_7/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ</url>
## </row>
##
## attr(,"class")
## [1] "XMLInternalNodeList" "XMLNodeList"
Convert to dataframe
data <- xmlSApply(rootNode,function(x) xmlSApply(x, xmlValue))
# Convert the extracted data into a data frame:
book_xml_table <- data.frame(t(data),row.names=NULL)
book_xml_table
## Title
## 1 Becoming
## 2 Dash & Lily's Book of Dares
## 3 Harry Potter and the Goblet of Fire: The Illustrated Edition
## Authors Rating Published_Year
## 1 Michelle Obama 4.9 2018
## 2 David Levithan,Rachel Cohn 4.1 2010
## 3 J. K. Rowling,Jim Kay NA 2019
## url
## 1 https://www.amazon.com/Becoming-Michelle-Obama/dp/1524763136/ref=zg_bs_books_7/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ
## 2 https://www.amazon.com/gp/product/0375866590/ref=x_gr_e_friend_sout_bb?ie=UTF8&tag=x_gr_e_friend_sout_bb-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0375866590&SubscriptionId=1MGPYB6YW3HWK55XCGG2
## 3 https://www.amazon.com/Harry-Potter-Goblet-Fire-Illustrated/dp/0545791421/ref=zg_bs_books_19/144-2021759-7862869?_encoding=UTF8&psc=1&refRID=NS10DER5K6QPN21GXDJJ
The 3 dataframes from xml, html and json files are 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.