One of the best parts of developing HTML elements in R is the ability to create reusable layouts / elements, grids, through functions. (example below)
But I haven't found a great way to quickly go from raw HTML to R code. I've attempted to use rvest::read_html() %>% purrr::as_list()
and recursively apply a custom function to each nested element, but often ran into unintended results.
I also tried to create a regex
match and replace process, but found there wasn't a great way to do this.
Ideally I'd like to create a function that parses raw html and prints the body of the function to the console (for quick prototyping and testing).
Has anyone tried to approach this or knows of anything that might work for this use case?
raw_html <- '
<div class="card text-white bg-primary mb-3" style="max-width: 18rem;">
<div class="card-header">Header</div>
<div class="card-body">
<h5 class="card-title">Primary card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card\'s content.</p>
</div>
</div>
'
new_fun <- function(card_head = "", card_title = "", cart_text = "") {
# what id like to print to console via function
div(
div(card_head,
class = "card-header"
),
div(
h5(card_title,
class = "card-title"
),
p(card_text,
class = "card-text"
),
class = "card-body"
),
class = "card text-white bg-primary mb-3", style = "max-width: 18rem;"
)
}
new_fun(
"Header","Primary card title",
"Some quick example text to build on"
)