Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
Warning: packages 'rvest', 'jsonlite' are in use and will not be installed
library(rvest)library(jsonlite)html_content <-'<!DOCTYPE html><html><head><title>Personal Finance Books</title></head><body><h1>Personal Finance and Financial Literacy</h1><table border="1"><tr><th>title</th><th>authors</th><th>year</th><th>publisher</th><th>genre</th></tr><tr><td>The Psychology of Money</td><td>Morgan Housel</td><td>2020</td><td>Harriman House</td><td>Personal Finance</td></tr><tr><td>Your Money or Your Life</td><td>Vicki Robin and Joe Dominguez</td><td>1992</td><td>Viking</td><td>Personal Finance</td></tr><tr><td>The Total Money Makeover</td><td>Dave Ramsey</td><td>2003</td><td>Thomas Nelson</td><td>Personal Finance</td></tr></table></body></html>'writeLines(html_content, "books.html")json_content <-'[ { "title": "The Psychology of Money", "authors": "Morgan Housel", "year": 2020, "publisher": "Harriman House", "genre": "Personal Finance" }, { "title": "Your Money or Your Life", "authors": "Vicki Robin and Joe Dominguez", "year": 1992, "publisher": "Viking", "genre": "Personal Finance" }, { "title": "The Total Money Makeover", "authors": "Dave Ramsey", "year": 2003, "publisher": "Thomas Nelson", "genre": "Personal Finance" }]'writeLines(json_content, "books.json")# Load HTMLhtml_df <-read_html("books.html") |>html_element("table") |>html_table() |>as.data.frame()# Load JSONjson_df <-fromJSON("books.json") |>as.data.frame()# Display the dataprint(html_df)
title authors year publisher
1 The Psychology of Money Morgan Housel 2020 Harriman House
2 Your Money or Your Life Vicki Robin and Joe Dominguez 1992 Viking
3 The Total Money Makeover Dave Ramsey 2003 Thomas Nelson
genre
1 Personal Finance
2 Personal Finance
3 Personal Finance
print(json_df)
title authors year publisher
1 The Psychology of Money Morgan Housel 2020 Harriman House
2 Your Money or Your Life Vicki Robin and Joe Dominguez 1992 Viking
3 The Total Money Makeover Dave Ramsey 2003 Thomas Nelson
genre
1 Personal Finance
2 Personal Finance
3 Personal Finance
# Standardize column namesnames(html_df) <-tolower(names(html_df))names(json_df) <-tolower(names(json_df))# Standardize yearhtml_df$year <-as.integer(html_df$year)json_df$year <-as.integer(json_df$year)# Match column orderjson_df <- json_df[, names(html_df), drop =FALSE]# Compareidentical(html_df, json_df)
[1] TRUE
getwd()
[1] "C:/Users/Reggie/OneDrive/Documentos/Week7"
list.files(pattern ="books")
[1] "books.html" "books.json"
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).