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(jsonlite)
library(htmltools)
library(htmlTable)
library(RCurl)
## Loading required package: bitops
library (knitr)
library(DT)
library(dplyr)
##
## 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
Loading the file from my GutHub
MyBooksHtml <- getURL("https://raw.githubusercontent.com/Emahayz/Data-607-Class/master/MyBooks.html")
Processing
MyBooksHtml<- readHTMLTable(MyBooksHtml, header = TRUE)
str(MyBooksHtml)
## List of 1
## $ NULL:'data.frame': 3 obs. of 6 variables:
## ..$ Title : Factor w/ 3 levels "Competing for the Future",..: 2 3 1
## ..$ Author : Factor w/ 3 levels "Gary Hamel and C.K. Prahalad",..: 2 3 1
## ..$ Publisher: Factor w/ 3 levels "CURRENCY: THE CROWN PUBLISHING GROUP",..: 1 3 2
## ..$ Year : Factor w/ 3 levels "1988","1996",..: 1 3 2
## ..$ City : Factor w/ 1 level "New York": 1 1 1
## ..$ Rating : Factor w/ 3 levels "4.3","4.4","4.5": 3 2 1
Viewing the dataframe
MyBooksHtml
## $`NULL`
## Title Author
## 1 Economics in One Lesson Henry Hazlitt
## 2 Freakonomics Steven D. Levitt and Stephen J. Dubner
## 3 Competing for the Future Gary Hamel and C.K. Prahalad
## Publisher Year City Rating
## 1 CURRENCY: THE CROWN PUBLISHING GROUP 1988 New York 4.5
## 2 William Morrow: HarperCollins 2005 New York 4.4
## 3 Harvard Business School Press 1996 New York 4.3
Loading the file from my GutHub
MyBooksXML <- getURL("https://raw.githubusercontent.com/Emahayz/Data-607-Class/master/MyXMLBooks.xml")
Processing
MyBooksXML.parse <- xmlParse(MyBooksXML)
MyBooksXML.root <- xmlRoot(MyBooksXML.parse)
MyBooksXML.xml <- xmlToDataFrame(MyBooksXML, stringsAsFactors = FALSE)
str(MyBooksXML.xml)
## 'data.frame': 3 obs. of 6 variables:
## $ Title : chr "Economics in One Lesson" "Freakonomics" "Competing for the Future"
## $ Authors : chr "Henry Hazlitt" "Steven D. Levitt and Stephen J. Dubner" "Gary Hamel and C.K. Prahalad"
## $ Publisher: chr "CURRENCY: THE CROWN PUBLISHING GROUP" "William Morrow: HarperCollins" "Harvard Business School Press"
## $ Year : chr "1988" "2005" "1996"
## $ City : chr "New York" "New York" "New York"
## $ Rating : chr "4.5" "4.4" "4.3"
MyBooksXML.xml
## Title Authors
## 1 Economics in One Lesson Henry Hazlitt
## 2 Freakonomics Steven D. Levitt and Stephen J. Dubner
## 3 Competing for the Future Gary Hamel and C.K. Prahalad
## Publisher Year City Rating
## 1 CURRENCY: THE CROWN PUBLISHING GROUP 1988 New York 4.5
## 2 William Morrow: HarperCollins 2005 New York 4.4
## 3 Harvard Business School Press 1996 New York 4.3
Viewing the dataframe
datatable(MyBooksXML.xml)
Loading the file from my GutHub
MyBooksJSON <- getURL("https://raw.githubusercontent.com/Emahayz/Data-607-Class/master/MyBooks.json")
MyBooksJSON<- fromJSON(MyBooksJSON)
Processing
MyBooksJSON <- as.data.frame(MyBooksJSON)
str(MyBooksJSON)
## 'data.frame': 3 obs. of 6 variables:
## $ Title : chr "Economics in One Lesson" "Freakonomics" "Competing for the Future"
## $ Author : chr "Henry Hazlitt" "Steven D. Levitt and Stephen J. Dubner" "Gary Hamel and C.K. Prahalad"
## $ Publisher: chr "CURRENCY: THE CROWN PUBLISHING GROUP" "William Morrow: HarperCollins" "Harvard Business School Press"
## $ Year : int 1988 2005 1996
## $ City : chr "New York" "New York" "New York"
## $ Rating : num 4.5 4.4 4.3
Viewing the dataframe
datatable(MyBooksJSON)
Although the structures are the same for the three files, I couldn’t get the datatable to work for the HTML file, the display for the XML and JSON are nicer with the datatable.
The HTML have the structure as Factors, the XML file has character while the JSON file has four characters, one integer and one number. However, the three files displayed the same information though not exactly identical.