Check if running in RStudio and then set the working directory
if (rstudioapi::isAvailable()) {
current_dir <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(current_dir)
cat("Working directory set to:", getwd(), "\n")
} else {
cat("rstudioapi not available. Please set the working directory manually using setwd().\n")
}
## rstudioapi not available. Please set the working directory manually using setwd().
# Install zoo if not already installed
if (!requireNamespace("xml2", quietly = TRUE)) {install.packages("xml2")}
if (!requireNamespace("jsonlite", quietly = TRUE)) {install.packages("jsonlite")}
if (!requireNamespace("rvest", quietly = TRUE)) install.packages("rvest")
if (!requireNamespace("openintro", quietly = TRUE)) install.packages("openintro")
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
# Load HTML file
html_data <- read_html("books.html")
table_data <- html_table(html_nodes(html_data, "table")[[1]])
print("HTML Data:")
## [1] "HTML Data:"
## # A tibble: 3 × 3
## Title Authors Interesting Attribut…¹
## <chr> <chr> <chr>
## 1 The Politics of Innovation: Why Some Countries… "Mark … Explores how politics…
## 2 How the West Came to Rule: The Geopolitical Or… "Alexa… Challenges Eurocentri…
## 3 The Fourth Industrial Revolution "Klaus… Discusses technologic…
## # ℹ abbreviated name: ¹`Interesting Attributes`
# Load XML file using xml2
xml_data <- read_xml("books.xml")
xml_books <- xml_find_all(xml_data, "//book")
books_list <- lapply(xml_books, function(book) {
list(
title = xml_text(xml_find_first(book, "title")),
authors = xml_text(xml_find_all(book, "authors/author")),
attributes = xml_text(xml_find_all(book, "attributes/attribute"))
)
})
print("XML Data:")
## [1] "XML Data:"
## [[1]]
## [[1]]$title
## [1] "The Politics of Innovation: Why Some Countries Are Better Than Others at Science and Technology"
##
## [[1]]$authors
## [1] "Mark Zachary Taylor"
##
## [[1]]$attributes
## [1] "Explores how politics, rather than institutions, drive S&T competitiveness"
##
##
## [[2]]
## [[2]]$title
## [1] "How the West Came to Rule: The Geopolitical Origins of Capitalism"
##
## [[2]]$authors
## [1] "Alexander Anievas" "Kerem Nişancıoğlu"
##
## [[2]]$attributes
## [1] "Challenges Eurocentric views by arguing capitalism's rise was a global process"
##
##
## [[3]]
## [[3]]$title
## [1] "The Fourth Industrial Revolution"
##
## [[3]]$authors
## [1] "Klaus Schwab"
##
## [[3]]$attributes
## [1] "Discusses technological advancements shaping the future"
## [1] "JSON Data:"
## $books
## title
## 1 The Politics of Innovation: Why Some Countries Are Better Than Others at Science and Technology
## 2 How the West Came to Rule: The Geopolitical Origins of Capitalism
## 3 The Fourth Industrial Revolution
## authors
## 1 Mark Zachary Taylor
## 2 Alexander Anievas, Kerem Nişancıoğlu
## 3 Klaus Schwab
## attributes
## 1 Explores how politics, rather than institutions, drive S&T competitiveness
## 2 Challenges Eurocentric views by arguing capitalism's rise was a global process
## 3 Discusses technological advancements shaping the future