Libraries
library(jsonlite)
library(tidyr)
library(xml2)
library(purrr)
##
## Attaching package: 'purrr'
## The following object is masked from 'package:jsonlite':
##
## flatten
library(rvest)
##
## Attaching package: 'rvest'
## The following object is masked from 'package:purrr':
##
## pluck
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
Read JSON into data frame
JSource = 'https://raw.githubusercontent.com/BDavidoff/Data_607/master/books.json'
JFrame = fromJSON(JSource) %>% as.data.frame
JFrame
## name
## 1 The History of the Decline and Fall of the Roman Empire
## 2 Hyperion
## 3 Good Omens
## authors genre year origin_country
## 1 Edward Gibbon History 1776 England
## 2 Dan Simmons Science Fiction 1989 America
## 3 Neil Gaiman, Terry Pratchett Fantasy 1990 England
#NOTE: the authors column doesn't read well in the raw output because this is a list object instead of a character deliminated string. I think that this is a better choice because it will make it easier to do data manipulation, which is more important than display in R Studio.
Read XML into data frame
XSource = 'https://raw.githubusercontent.com/BDavidoff/Data_607/master/books.xml'
XData = read_xml(XSource)
XFrame = xml_find_all(XData, ".//book") %>%
map_df(function(x) {
list(
name=xml_find_first(x, ".//name") %>% xml_text(),
authors=list(xml_find_all(x, './/author') %>% xml_text()),
genre=xml_find_first(x, ".//genre") %>% xml_text(),
year=xml_find_first(x, ".//year") %>% xml_text(),
origin_country=xml_find_first(x, ".//origin_country") %>% xml_text()
)
})
XFrame
## # A tibble: 3 x 5
## name authors genre year origin_country
## <chr> <list> <chr> <chr> <chr>
## 1 The History of the Decline and Fall~ <chr [1~ History 1776 England
## 2 Hyperion <chr [1~ Science Fi~ 1989 America
## 3 Good Omens <chr [2~ Fantasy 1990 England
Read HTML into data frame
HSource = 'https://raw.githubusercontent.com/BDavidoff/Data_607/master/books.html'
HData = read_html(HSource)
HFrame = html_nodes(HData, "table") %>%
html_table(fill = TRUE)
HFrame
## [[1]]
## Name
## 1 The History of the Decline and Fall of the Roman Empire
## 2 Hyperion
## 3 Good Omens
## Authors Genre Origin_Country NA
## 1 Edward Gibbon History 1776 England
## 2 Dan Simmons Science Fiction 1989 America
## 3 Neil Gaiman, Terry Partchet Fantasy 1990 England
Programatically compare the data frames
all_equal(JFrame, XFrame)
## [1] TRUE
the all_equal function confirms that the json and xml frames are identical, the html frame having a different value for authors is obviously not the same.
Conclusions: I thought that the JSON dataframe was the easiest to construct, and since that is more or less the standard for data storage (not including SQL) these days, that is probably a good thing. I was surprised to see how much more difficult XML was to get into a frame than HTML was, considering that html usually isnโt used for this purpose, but it makes sense that this would be important in R as scraping HTML data would be very useful for getting large amounts of data off the web for the purposes of cleaning and processing.