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)
## Loading required package: bitops
library(knitr)
library(jsonlite)
library(htmltab)
## Warning: package 'htmltab' was built under R version 3.4.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(stringr)
use htmltab to parse out HTML data
html_data <- htmltab(doc = getURL("https://raw.githubusercontent.com/Eric66-99/DATA-607/master/test1.html"))
## Argument 'which' was left unspecified. Choosing first table.
for (i in 1:3) {
url <- rep(html_data$Amazon[i], 1)
html_data$Amazon[i] <- paste0("[", "Buy Amazon", "](", url, ")")
}
#use kable to nicely format the data
kable(html_data)
| Title | Authors | Publisher | YearPublished | Amazon | |
|---|---|---|---|---|---|
| 2 | Zen Mind, Beginner’s Mind: Informal Talks on Zen Meditation and Practice | Shunryu Suzuki (Author), Trudy Dixon (Editor), Huston Smith (Preface) | Shambhala | June 28, 2011 | Buy Amazon |
| 3 | Be Here Now | Ram Dass | HarperCollins | November 2, 2010 | Buy Amazon |
| 4 | Mindfulness, 25th anniversary edition | Ellen J. Langer | Da Capo Lifelong Books | October 14, 2014 | Buy Amazon |
use JSONLITE parse out JSON data
json_file <- getURL("https://raw.githubusercontent.com/Eric66-99/DATA-607/master/test1.json")
json_data <- fromJSON(paste(json_file, collapse=""))
jsonDF <- data.frame(json_data)
for (i in 1:3) {
url <- rep(jsonDF$mybooks.Amazon[i], 1)
jsonDF$Amazon[i] <- paste0("[", "Buy Amazon", "](", url, ")")
}
#Fix the Header Names
names(jsonDF)[names(jsonDF)=="mybooks.title"] <- "Title"
names(jsonDF)[names(jsonDF)=="mybooks.authors"] <- "Authors"
names(jsonDF)[names(jsonDF)=="mybooks.publisher"] <- "Publisher"
names(jsonDF)[names(jsonDF)=="mybooks.yearpublished"] <- "YearPublished"
jsonDF$mybooks.Amazon <- NULL
kable(jsonDF)
| Title | Authors | Publisher | YearPublished | Amazon |
|---|---|---|---|---|
| Zen Mind, Beginner’s Mind: Informal Talks on Zen Meditation and Practice | Shunryu Suzuki (Author), Trudy Dixon (Editor), Huston Smith (Preface) | Shambhala | June 28, 2011 | Buy Amazon |
| Be Here Now | Ram Dass | HarperCollins | November 2, 2010 | Buy Amazon |
| Mindfulness, 25th anniversary edition | Ellen J. Langer | Da Capo Lifelong Books | October 14, 2014 | Buy Amazon |
used XML library to parse XML data.
rawXML <- xmlParse<- getURL("https://raw.githubusercontent.com/Eric66-99/DATA-607/master/test1.xml")
xmlDF <- xmlToDataFrame(rawXML)
rXML <- xmlToList(rawXML)
xmlDF$Amazon1 <- NULL
for (i in 1:3) {
url = rep(xmlDF$Amazon[i], 1)
xmlDF$Amazon1[i] <- paste0("[", "Buy Amazon", "](", url, ")")
}
#Fix the Header Names
names(xmlDF)[names(xmlDF)=="title"] <- "Title"
names(xmlDF)[names(xmlDF)=="mybooks.authorname"] <- "Authors"
names(xmlDF)[names(xmlDF)=="publisher"] <- "Publisher"
names(xmlDF)[names(xmlDF)=="yearpublished"] <- "YearPublished"
names(xmlDF)[names(xmlDF)=="Amazon1"] <- "Amazon"
xmlDF$Amazon <- NULL
kable(xmlDF)
| Title | authorname | Publisher | YearPublished | Amazon |
|---|---|---|---|---|
| Zen Mind, Beginner’s Mind: Informal Talks on Zen Meditation and Practice | Shunryu Suzuki (Author), Trudy Dixon (Editor), Huston Smith (Preface) | Shambhala | June 28, 2011 | Buy Amazon |
| Be Here Now | Ram Dass | HarperCollins | November 2, 2010 | Buy Amazon |
| Mindfulness, 25th anniversary edition | Ellen J. Langer | Da Capo Lifelong Books | October 14, 2014 | Buy Amazon |