##Read XML
library(xml2)
## Warning: package 'xml2' was built under R version 4.0.5
library(RCurl)
## Warning: package 'RCurl' was built under R version 4.0.5
library(xmlconvert)
## Warning: package 'xmlconvert' was built under R version 4.0.5
library(rvest)
library(jsonlite)
library(purrr)
##
## Attaching package: 'purrr'
## The following object is masked from 'package:jsonlite':
##
## flatten
url <- "https://raw.githubusercontent.com/seyi116/data607/main/books.xml"
xml_data <- read_xml(url)
books <- xml_data %>%
xml_find_all(".//Book") %>%
map_df(~{
title <- .x %>% xml_find_first(".//title") %>% xml_text()
author <- .x %>% xml_find_first(".//Author") %>% xml_text()
year <- .x %>% xml_find_first(".//Year") %>% xml_text()%>% as.numeric()
publisher <- .x %>% xml_find_first(".//Publisher") %>% xml_text()
data.frame(title = title, year = year, author = author, publisher = publisher)
})
print(books)
## title year author publisher
## 1 R for Data Science 2017 Garrett Grolemund, Hadley Wickham O'Reilly Media
## 2 Think Python 2002 Allen B. Downey O'Reilly Media
## 3 Real Life 2021 Brandon Taylor riverhead
url <- "https://raw.githubusercontent.com/seyi116/data607/main/books.html"
html_data <- read_html(url)
tables <- html_data %>% html_table(fill = TRUE)
books_df <- as.data.frame(tables[[1]])
print(books_df)
## Title Year Author Publisher
## 1 R for Data Science 2017 Garrett Grolemund, Hadley Wickham O'Reilly Media
## 2 Think Python 2002 Allen B. Downey O'Reilly Media
## 3 Real Life 2021 Brandon Taylor Riverhead
###Read JSON
url <- "https://raw.githubusercontent.com/seyi116/data607/main/books.json"
json_data <- fromJSON(url)
# Convert JSON data to dataframe
books_df <- as.data.frame(json_data)
# Print the dataframe
print(books_df)
## Title Year Author Publisher
## 1 R or Data Science 2017 Garrett Grolemund, Hadley Wickham O'Reilly Media
## 2 Think Python 2002 Allen B. Downey O'Reilly Media
## 3 Real Life 2021 Brandon Taylor Riverhead
If loaded properly the data frames load properly