# Packages to be used
library(tidyverse)
library(rvest) # used to manipulate HTML
#> Warning: package 'rvest' was built under R version 4.0.4
library(XML)
#> Warning: package 'XML' was built under R version 4.0.4
library(xml2)
library(jsonlite)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?
Read book data from an HTML file and store it in a dataframe
books_html <- rvest::read_html("Books.html")
df_html <- books_html %>%
html_element("table") %>%
html_table()Read book data from an XML file and store it in a dataframe
books_xml <- XML::xmlParse("Books.xml")
root_xml <- XML::xmlRoot(books_xml)
df_xml <- xmlToDataFrame(root_xml)Read book data from an JSON file and store it in a dataframe
books_json <- jsonlite::read_json("Books.json", simplifyVector = TRUE)
df_json <- books_json$BooksThe resulting data types for HTML, XML and JSON sources are different.
class(books_html)[1] “xml_document” “xml_node”
class(books_xml)[1] “XMLInternalDocument” “XMLAbstractDocument”
class(books_json)[1] “list”
The resulting content of the dataframes for all three sources are different.
Dataframe output from HTML source:
The authors columns are repeated for each author and are filled with “NA” when fewer authors exist
| BookTitle | BookISBN13 | BookISBN10 | Publisher | Pages | Authors | Authors | Authors | Authors | Authors |
|---|---|---|---|---|---|---|---|---|---|
| The Data Warehouse Lifecycle Toolkit 2nd Edition | 978-0470149775 | 470149779 | Wiley; 2nd edition (January 10, 2008) | 672 | Ralph Kimball | Margy Ross | Warren Thornthwaite | Joy Mundy | Bob Becker |
| T-SQL Window Functions: For data analysis and beyond (Developer Reference) 2nd Edition | 978-0135861448 | 135861446 | Microsoft Press; 2nd edition (November 4, 2019) | 352 | Itzik Ben-Gan | NA | NA | NA | NA |
| Microsoft Power BI Quick Start Guide: Build dashboards and visualizations to make your data come to | |||||||||
| life | 978-1789138221 | 1789138221 | Packt Publishing (July 30, 2018) | 200 | Devin Knight | Brian Knight | Mitchell Pearson | Manuel Quintana | NA |
Dataframe output from XML source:
The authors names are combined in one column but they are separated by single spaces
| BookTitle | BookISBN13 | BookISBN10 | Publisher | Pages | Authors |
|---|---|---|---|---|---|
| The Data Warehouse Lifecycle Toolkit 2nd Edition | 978-0470149775 | 0470149779 | Wiley; 2nd edition (January 10, 2008) | 672 | Ralph KimballMargy RossWarren ThornthwaiteJoy MundyBob Becker |
| T-SQL Window Functions: For data analysis and beyond (Developer Reference) 2nd Edition | 978-0135861448 | 0135861446 | Microsoft Press; 2nd edition (November 4, 2019) | 352 | Itzik Ben-Gan |
| Microsoft Power BI Quick Start Guide: Build dashboards and visualizations to make your data come to life | 978-1789138221 | 1789138221 | Packt Publishing (July 30, 2018) | 200 | Devin KnightBrian KnightMitchell PearsonManuel Quintana |
Dataframe output from JSON source:
The authors names are combined in one column but they are separated by commas
| BookTitle | BookISBN13 | BookISBN10 | Publisher | Pages | Authors |
|---|---|---|---|---|---|
| The Data Warehouse Lifecycle Toolkit 2nd Edition | 978-0470149775 | 0470149779 | Wiley; 2nd edition (January 10, 2008) | 672 | Ralph Kimball , Margy Ross , Warren Thornthwaite, Joy Mundy , Bob Becker |
| T-SQL Window Functions: For data analysis and beyond (Developer Reference) 2nd Edition | 978-0135861448 | 0135861446 | Microsoft Press; 2nd edition (November 4, 2019) | 352 | Itzik Ben-Gan |
| Microsoft Power BI Quick Start Guide: Build dashboards and visualizations to make your data come to life | 978-1789138221 | 1789138221 | Packt Publishing (July 30, 2018) | 200 | Devin Knight , Brian Knight , Mitchell Pearson, Manuel Quintana |
The resulting data types for XML and JSON are the same, but the HTML is not.
class(df_html)[1] “tbl_df” “tbl” “data.frame”
class(df_xml)[1] “data.frame”
class(df_json)[1] “data.frame”