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]
Possibly needed Libraries
library(data.table)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::between() masks data.table::between()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks data.table::first()
## ✖ lubridate::hour() masks data.table::hour()
## ✖ lubridate::isoweek() masks data.table::isoweek()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks data.table::last()
## ✖ lubridate::mday() masks data.table::mday()
## ✖ lubridate::minute() masks data.table::minute()
## ✖ lubridate::month() masks data.table::month()
## ✖ lubridate::quarter() masks data.table::quarter()
## ✖ lubridate::second() masks data.table::second()
## ✖ purrr::transpose() masks data.table::transpose()
## ✖ lubridate::wday() masks data.table::wday()
## ✖ lubridate::week() masks data.table::week()
## ✖ lubridate::yday() masks data.table::yday()
## ✖ lubridate::year() masks data.table::year()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rvest) #HTML
##
## Attaching package: 'rvest'
##
## The following object is masked from 'package:readr':
##
## guess_encoding
library(jsonlite) #JSON
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
library(xml2) #XML
HTML, converted from csv; (https://tableizer.journalistopia.com/)
raw_html <- "https://raw.githubusercontent.com/RonBalaban/CUNY-SPS-R/main/Books.html"
books_html <- html <- read_html(raw_html)
#-------------------------------------------------------------------------------
# Make into frame
html_table <- books_html %>%
html_element("table") %>%
html_table()
html_table
## # A tibble: 3 × 4
## Title Author Genre Year
## <chr> <chr> <chr> <int>
## 1 Rhythm of War Brandon Sanderson Fantasy Fiction 2020
## 2 Quiet Susan Cain Psychology 2012
## 3 The Witcher Omnibus Paul Tobin, Joe Querio Fantasy Fiction 2020
JSON, converted from csv; (https://www.convertcsv.com/csv-to-json.htm)
raw_json <- "https://raw.githubusercontent.com/RonBalaban/CUNY-SPS-R/main/books_JSON"
books_json <- fromJSON(raw_json, flatten = TRUE)
books_json
## Title Author Genre Year
## 1 Rhythm of War Brandon Sanderson Fantasy Fiction 2020
## 2 Quiet Susan Cain Psychology 2012
## 3 The Witcher Omnibus Paul Tobin, Joe Querio Fantasy Fiction 2020
XML, converted from csv; (https://www.convertcsv.com/csv-to-xml.htm)
raw_xml <- "https://raw.githubusercontent.com/RonBalaban/CUNY-SPS-R/main/books_XML"
books_xml <- read_xml(raw_xml)
#-------------------------------------------------------------------------------
# Make into frame
# Extract required rows
title_nodes <- books_xml %>%
xml_find_all(".//row") %>%
xml_find_all(".//Title")
genre_nodes <- books_xml %>%
xml_find_all(".//row") %>%
xml_find_all(".//Genre")
author_nodes <- books_xml %>%
xml_find_all(".//row") %>%
xml_find_all(".//Author")
year_nodes <- books_xml %>%
xml_find_all(".//row") %>%
xml_find_all(".//Year")
# Extract text from above
book_title <- xml_text(title_nodes)
book_genre <- xml_text(genre_nodes)
book_author <- xml_text(author_nodes)
book_year <- xml_text(year_nodes)
# Make frame
xml_table <- tibble(Title = book_title, Author = book_author, Genre = book_genre, Year= book_year)
xml_table
## # A tibble: 3 × 4
## Title Author Genre Year
## <chr> <chr> <chr> <chr>
## 1 Rhythm of War Brandon Sanderson Fantasy Fiction 2020
## 2 Quiet Susan Cain Psychology 2012
## 3 The Witcher Omnibus Paul Tobin, Joe Querio Fantasy Fiction 2020
typeof(html_table)
## [1] "list"
typeof(books_json)
## [1] "list"
typeof(xml_table)
## [1] "list"
The 3 formats all have the same dataframe output.