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().

Library

# 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")
# Load necessary libraries
library(xml2)
library(jsonlite)
library(rvest)
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(RCurl)
# 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:"
print(table_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:"
print(books_list)
## [[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"
# Load JSON file
json_data <- fromJSON("books.json")
print("JSON Data:")
## [1] "JSON Data:"
print(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
LS0tCm91dHB1dDoKICBvcGVuaW50cm86OmxhYl9yZXBvcnQ6IGRlZmF1bHQKICBodG1sX2RvY3VtZW50OiBkZWZhdWx0Ci0tLQoKCgoKQ2hlY2sgaWYgcnVubmluZyBpbiBSU3R1ZGlvIGFuZCB0aGVuIHNldCB0aGUgd29ya2luZyBkaXJlY3RvcnkKYGBge3J9CmlmIChyc3R1ZGlvYXBpOjppc0F2YWlsYWJsZSgpKSB7CiAgY3VycmVudF9kaXIgPC0gZGlybmFtZShyc3R1ZGlvYXBpOjpnZXRBY3RpdmVEb2N1bWVudENvbnRleHQoKSRwYXRoKQogIHNldHdkKGN1cnJlbnRfZGlyKQogIGNhdCgiV29ya2luZyBkaXJlY3Rvcnkgc2V0IHRvOiIsIGdldHdkKCksICJcbiIpCn0gZWxzZSB7CiAgY2F0KCJyc3R1ZGlvYXBpIG5vdCBhdmFpbGFibGUuIFBsZWFzZSBzZXQgdGhlIHdvcmtpb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