Creating a file with data of my favorite books in HTML format.
# Define the data set
books <- data.frame(
book = c("Atomic Habits", "Attached", "The Kite Runner"),
authors = c("James Clear", "Dr. Amir Levine, Rachel Heller", "Khaled Hosseini"),
pages = c(320, 304, 372),
genre = c("Self Help", "Psychology", "Historical Fiction")
)
# Write the data to an HTML file
write.table(books, "books.html", sep = "\t", row.names = FALSE, quote = FALSE)
Creatig the data set in XML Format
library(XML)
# Define the data set
books <- data.frame(
book = c("Atomic Habits", "Attached", "The Kite Runner"),
authors = c("James Clear", "Dr. Amir Levine, Rachel Heller", "Khaled Hosseini"),
pages = c(320, 304, 372),
genre = c("Self Help", "Psychology", "Historical Fiction")
)
# Create an XML document
books_xml <- newXMLDoc()
books_root <- newXMLNode("books", doc = books_xml)
# Loop through the data and add elements to the XML
for (i in 1:nrow(books)) {
book_node <- newXMLNode("book", parent = books_root)
newXMLNode("title", books$book[i], parent = book_node)
newXMLNode("authors", books$authors[i], parent = book_node)
newXMLNode("pages", books$pages[i], parent = book_node)
newXMLNode("genre", books$genre[i], parent = book_node)
}
# Save the XML to a file
saveXML(books_xml, file = "books.xml")
## [1] "books.xml"
Creating the data in json format
library(jsonlite)
# Define the data set
books <- data.frame(
book = c("Atomic Habits", "Attached", "The Kite Runner"),
authors = c("James Clear", "Dr. Amir Levine, Rachel Heller", "Khaled Hosseini"),
pages = c(320, 304, 372),
genre = c("Self Help", "Psychology", "Historical Fiction")
)
# Convert the data to a JSON string
books_json <- toJSON(books, pretty = TRUE)
# Write the JSON to a file
writeLines(books_json, "books.json")
Loadin the data in HTML format
# Read data from HTML file
books_html <- read.table("books.html", sep = "\t", header = TRUE, stringsAsFactors = FALSE)
# View the data
head(books_html)
## book authors pages genre
## 1 Atomic Habits James Clear 320 Self Help
## 2 Attached Dr. Amir Levine, Rachel Heller 304 Psychology
## 3 The Kite Runner Khaled Hosseini 372 Historical Fiction
Loading the data in XML format
# Load the XML package
library(XML)
# Read data from XML file
books_xml <- xmlToDataFrame("books.xml")
# View the data
head(books_xml)
## title authors pages genre
## 1 Atomic Habits James Clear 320 Self Help
## 2 Attached Dr. Amir Levine, Rachel Heller 304 Psychology
## 3 The Kite Runner Khaled Hosseini 372 Historical Fiction
Loading the data in json format
# Read data from JSON file
books_json <- fromJSON("books.json")
head(books_json)
## book authors pages genre
## 1 Atomic Habits James Clear 320 Self Help
## 2 Attached Dr. Amir Levine, Rachel Heller 304 Psychology
## 3 The Kite Runner Khaled Hosseini 372 Historical Fiction
After creating the books data set in html, XML, and json form, and viewing the tables, we can see that the html and the json format are identical. The columns for the html and json format are in the following order: Book, Authors, Pages, Genre, where as the XML file has columns named: Title, Authors, Pages, Genre. Also the layout of the html and json files are similar where as the layout of the XML file is slightly different.