HTML
library(XML)
url = "c:/data/books.html"
htmlbooks = readHTMLTable(url,stringsAsFactors = FALSE)
htmlbooks.df = as.data.frame(htmlbooks)
colnames(htmlbooks.df) = c("Book Title","Author","Pages","Amazon Stars")
kable(htmlbooks.df)
| The Hitchhiker’s Guide to the Galaxy |
Douglas Adams |
600 |
4.4 |
| Ender’s Game |
Orson Scott Card |
700 |
4.6 |
| The Dark Tower I |
Stephen King, Bing Bong |
800 |
4.2 |
XML
#xml = "https://raw.githubusercontent.com/jelikish/Cuny1/master/Fall2016/607/607-HW7/books.xml"
xml = "c:/data/books.xml"
xmlbook = xmlParse(xml)
root = xmlRoot(xmlbook)
xmlbooks.df = xmlToDataFrame(root)
colnames(xmlbooks.df) = c("Book Title","Author","Pages","Amazon Stars")
kable(xmlbooks.df)
| The Hitchhiker’s Guide to the Galaxy |
Douglas Adams |
600 |
4.4 |
| Ender’s Game |
Orson Scott Card |
700 |
4.6 |
| The Dark Tower I |
Stephen King,Bing Bong |
800 |
4.2 |
JSON
library(jsonlite)
library(plyr)
#json = "https://raw.githubusercontent.com/jelikish/Cuny1/master/Fall2016/607/607-HW7/books.json"
json = "c:/data/books.json"
jsonbooks = fromJSON(json)
jsonbooks.unlist <- sapply(jsonbooks[[1]], unlist)
jsonbooks.unlist[[2]][[3]]=paste(jsonbooks.unlist[[2]][[3]],",",jsonbooks.unlist[[2]][[4]])
jsonbooks.unlist[[2]] = list(jsonbooks.unlist[[2]][[1]],jsonbooks.unlist[[2]][[2]],jsonbooks.unlist[[2]][[3]])
jsonbooks.df <- do.call("rbind.fill", lapply(lapply(jsonbooks.unlist, t), data.frame, stringsAsFactors = FALSE))
jsonbooks.df = t(jsonbooks.df)
jsonbooks.df = as.data.frame(jsonbooks.df)
colnames(jsonbooks.df) = c("Book Title","Author","Pages","Amazon Stars")
kable(jsonbooks.df)
| X1 |
The Hitchhiker’s Guide to the Galaxy |
Douglas Adams |
600 |
4.4 |
| X2 |
Ender’s Game |
Orson Scott Card |
700 |
4.6 |
| X3 |
The Dark Tower I |
Stephen King , Bing Bong |
800 |
4.2 |