#Assignment – Working with XML and JSON in R
###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].
knitr::opts_chunk$set(echo = TRUE)
#load library
library(jsonlite)
library(xml2)
library(XML)
library(htmltab)
#JSON
d <- jsonlite::fromJSON(txt = "https://raw.githubusercontent.com/marjete/XML-JSON/main/data.json", simplifyDataFrame = TRUE)
df_json <- as.data.frame(d)
df_json
## books.title books.author books.year books.pages
## 1 tuesday with morrie Mitch Albom 1997 192
## 2 the kite runner Khaled Hosseini 2003 400
## 3 The Talisman Peter Straub and Stephen King 1984 656
str(df_json)
## 'data.frame': 3 obs. of 4 variables:
## $ books.title : chr " tuesday with morrie" "the kite runner" "The Talisman"
## $ books.author: chr "Mitch Albom" "Khaled Hosseini" "Peter Straub and Stephen King"
## $ books.year : chr "1997" "2003" "1984"
## $ books.pages : chr "192" "400" "656"
#XML
data_xml <- xml2::read_xml("https://raw.githubusercontent.com/marjete/XML-JSON/main/data.xml")
book_xml <- XML::xmlParse(data_xml)
df_xml <- xmlToDataFrame(nodes=getNodeSet(book_xml, "//book"))
df_xml
## title author
## 1 \n tuesday with morrie\n \n Mitch Albom\n
## 2 \n the kite runner\n \n Khaled Hosseini\n
## 3 \n The Talisman\n \n Peter Straub and Stephen King\n
## year pages
## 1 \n 1997\n \n 192\n
## 2 \n 2003\n \n 400\n
## 3 \n 1984\n \n 656\n
I’m not sure why xml has ‘/n’ - its the only one that doesnt look similar to the other file structures.
str(df_xml)
## 'data.frame': 3 obs. of 4 variables:
## $ title : chr "\n tuesday with morrie\n " "\n the kite runner\n " "\n The Talisman\n "
## $ author: chr "\n Mitch Albom\n " "\n Khaled Hosseini\n " "\n Peter Straub and Stephen King\n "
## $ year : chr "\n 1997\n " "\n 2003\n " "\n 1984\n "
## $ pages : chr "\n 192\n " "\n 400\n " "\n 656\n "
#HTML
html <- htmltab::htmltab(doc = "https://raw.githubusercontent.com/marjete/XML-JSON/main/data.html")
## Argument 'which' was left unspecified. Choosing first table.
html
## Title Author year Pages
## 2 tuesday with morrie Mitch Albom 1997 192
## 3 the kite runner Khaled Hosseini 2003 400
## 4 The Talisman Peter Straub and Stephen King 1984 656
str(html)
## 'data.frame': 3 obs. of 4 variables:
## $ Title : chr "tuesday with morrie" "the kite runner" "The Talisman"
## $ Author: chr "Mitch Albom" "Khaled Hosseini" "Peter Straub and Stephen King"
## $ year : chr "1997" "2003" "1984"
## $ Pages : chr "192" "400" "656"
#Conclusion: All the three dataframes should be identical the number of content (#‘3 obs. of 4 variables’) and by the type, all reflecting ‘chr’ characters. Although my xml file/ data does look different in the code above.