21.4 Named Components
li("What is the ", b("derivative"), "of $f(x) = 1 + 2\cos(3\pi x + 4)$?")
- aiming to classify
li
andb
as named components
dots_partition <- function(...) {
dots <- list2(...)
if (is.null(names(dots))) {
is_named <- rep(FALSE, length(dots))
} else {
is_named <- names(dots) != ""
}
list(
named = dots[is_named],
unnamed = dots[!is_named]
)
}
21.4.1 Check
str(dots_partition(company = "Posit",
software = "RStudio",
"DSLC",
"Cohort 9"))
#> List of 2
#> $ named :List of 2
#> ..$ company : chr "Posit"
#> ..$ software: chr "RStudio"
#> $ unnamed:List of 2
#> ..$ : chr "DSLC"
#> ..$ : chr "Cohort 9"
HTML Attributes
Found among the textbook’s source code
html_attributes <- function(list) {
if (length(list) == 0) return("")
attr <- map2_chr(names(list), list, html_attribute)
paste0(" ", unlist(attr), collapse = "")
}
html_attribute <- function(name, value = NULL) {
if (length(value) == 0) return(name) # for attributes with no value
if (length(value) != 1) stop("`value` must be NULL or length 1")
if (is.logical(value)) {
# Convert T and F to true and false
value <- tolower(value)
} else {
value <- escape_attr(value)
}
paste0(name, "='", value, "'")
}
escape_attr <- function(x) {
x <- escape.character(x)
x <- gsub("\'", ''', x)
x <- gsub("\"", '"', x)
x <- gsub("\r", ' ', x)
x <- gsub("\n", ' ', x)
x
}