Pick 3 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?
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.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(rvest)
##
## Attaching package: 'rvest'
##
## The following object is masked from 'package:readr':
##
## guess_encoding
library("jsonlite")
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
library(xml2)
rawlink_html<- 'https://raw.githubusercontent.com/rkasa01/DATA607_HW7_Files/main/BOOKS_RKASA.html'
html <- read_html(rawlink_html)
print(html)
## {html_document}
## <html>
## [1] <head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8 ...
## [2] <body>\n <table border="1">\n<tr>\n<th>Title</th>\n <th>Autho ...
HTML_df <- data.frame(html_table(html, fill = TRUE)[[1]])
print(HTML_df)
## Title
## 1 The Man Who Mistook His Wife for a Hat
## 2 Clinical Psychopharmacology Made Ridiculously Simple
## 3 When Breath Becomes Air
## Author
## 1 Oliver Sacks
## 2 John Preston, James Johnson
## 3 Paul Kalanithi
## Reason.1
## 1 I enjoy the writing style of Oliver Sacks.
## 2 I used this book through a challenging Psychopharmacology course in college.
## 3 This is an autobiography by neurosurgeon, Dr. Paul Kalanithi, with worsening lung cancer.
## Reason.2
## 1 This is a book of interesting case studies.
## 2 It's written in a humorous style.
## 3 It helped me navigate the death of a loved one with cancer.
## Reason.3
## 1 This book was gifted to me by one of my former mentors.
## 2 I got an A in the class!
## 3 It's one of the most beautiful books I have ever read and is very special to me.
rawlink_json<-'https://raw.githubusercontent.com/rkasa01/DATA607_HW7_Files/main/BOOKS_RKASA.json'
json <- fromJSON(rawlink_json)
print(json)
## Title
## 1 The Man Who Mistook His Wife for a Hat
## 2 Clinical Psychopharmacology Made Ridiculously Simple
## 3 When Breath Becomes Air
## Author
## 1 Oliver Sacks
## 2 John Preston, James Johnson
## 3 Paul Kalanithi
## Reason1
## 1 I enjoy the writing style of Oliver Sacks.
## 2 I used this book through a challenging Psychopharmacology course in college.
## 3 This is an autobiography by neurosurgeon, Dr. Paul Kalanithi, with worsening lung cancer.
## Reason2
## 1 This is a book of interesting case studies.
## 2 It's written in a humorous style.
## 3 It helped me navigate the death of a loved one with cancer.
## Reason3
## 1 This book was gifted to me by one of my former mentors.
## 2 I got an A in the class!
## 3 It's one of the most beautiful books I have ever read and is very special to me.
rawlink_xml<-'https://raw.githubusercontent.com/rkasa01/DATA607_HW7_Files/main/BOOKS_RKASA%202.xml'
xml_data <- read_xml(rawlink_xml)
print(xml_data)
## {xml_document}
## <Books>
## [1] <Book>\n <Title>The Man Who Mistook His Wife for a Hat</Title>\n <Autho ...
## [2] <Book>\n <Title>Clinical Psychopharmacology Made Ridiculously Simple</Ti ...
## [3] <Book>\n <Title>When Breath Becomes Air</Title>\n <Author>Paul Kalanith ...
#Extracting Data - XML
Title <- xml_data %>% xml_find_all(".//Title") %>% xml_text()
Author <- xml_data %>% xml_find_all(".//Author") %>% xml_text()
Reason1 <- xml_data %>% xml_find_all(".//Reason1") %>% xml_text()
Reason2 <- xml_data %>% xml_find_all(".//Reason2") %>% xml_text()
Reason3 <- xml_data %>% xml_find_all(".//Reason3") %>% xml_text()
xml_df <- data.frame(
Title = Title,
Author = Author,
Reason1 = Reason1,
Reason2 = Reason2,
Reason3 = Reason3
)
print(xml_df)
## Title
## 1 The Man Who Mistook His Wife for a Hat
## 2 Clinical Psychopharmacology Made Ridiculously Simple
## 3 When Breath Becomes Air
## Author
## 1 Oliver Sacks
## 2 John Preston, James Johnson
## 3 Paul Kalanithi
## Reason1
## 1 I enjoy the writing style of Oliver Sacks.
## 2 I used this book through a challenging Psychopharmacology course in college.
## 3 This is an autobiography by neurosurgeon, Dr. Paul Kalanithi, with worsening lung cancer.
## Reason2
## 1 This is a book of interesting case studies.
## 2 It's written in a humorous style.
## 3 It helped me navigate the death of a loved one with cancer.
## Reason3
## 1 This book was gifted to me by one of my former mentors.
## 2 I got an A in the class!
## 3 It's one of the most beautiful books I have ever read and is very special to me.
#Conclusion
To conclude, the dataframes are not similar when they are first read in R. After some manipulation though, they can be made similar. For example, with XML I had to extract the data, whereas the JSON data was readily available for the dataframe format. Additionally,the HTML data had to be converted to a dataframe, in order to print out as a dataframe.