The goal of this project is to learn how to create and read in tables in the JSON, HTML, and XML formats. In this project I selected three of my favorite books, one of which has two authors.
Calling in the library RJSON.
library(rjson)
To read in the JSON table and convert to data-frame.
my_fav_books <- fromJSON(file = "my_three_books.JSON")
my_fav_books <- as.data.frame(my_fav_books)
my_fav_books
## id Author1 Author2 Title Year Genre
## 1 1 James Clavel NA King Rat 1962 Historical Novel
## 2 2 Orson Scott Card NA Ender's Game 1985 Science Fiction
## 3 3 Stephen King Peter Straub The Talisman 1984 Fantasy
To load the package XML.
library(XML)
## Warning: package 'XML' was built under R version 4.1.3
To read in the HTML table and convert to data-frame.
my_fav_books2 <- readHTMLTable(doc = "my_three_books.html", as.data.frame = TRUE)
my_fav_books2
## $`NULL`
## id Author1 Author2 Title Year Genre
## 1 1 James Clavel NA King Rat 1962 Historical Novel
## 2 2 Orson Scott Card NA Ender's Game 1985 Science Fiction
## 3 3 Stephen King Peter Straub The Talisman 1984 Fantasy
To read in the XML table and convert to data-frame.
my_fav_books3 <- xmlToDataFrame(doc = "my_three_books.xml")
my_fav_books3
## id Author1 Author2 Title Year Genre
## 1 1 James Clavel NA King Rat 1962 Historical Novel
## 2 2 Orson Scott Card NA Ender's Game 1985 Science Fiction
## 3 3 Stephen King Peter Straub The Talisman 1984 Fantasy
Comparing the JSON to HTML and XM, once converted to data-frame all are identical. Further, however please note I had to name the columns identically and in the same case for the column headers to be the same.