`%||%` <- function(x, y) {
if (is.null(x)) {
y
} else {
x
}
}
#' Convert a string to title case
#'
#' This function replaces a sequence of non alpha-numeric characters to a single
#' space, and applies title case to the remaining words.
#'
#' @param x A character vector.
#'
#' @return A character vector.
#' @keywords internal
#' @examples
#' to_title(c("a.string", "ANOTHER_string"))
#' to_title(c("a.string", "another_string", "b.STRING"))
#' @noRd
to_title <- function(x) {
to_title_one <- function(x) {
words <- tolower(unlist(strsplit(x, "[^[:alnum:]]+")))
# `toTitleCase()` with "a" returns "a", not "A" (a bug in this context)
words <- capitalize_single_letters(tools::toTitleCase(words))
paste(words, collapse = " ")
}
unlist(lapply(x, to_title_one))
}
capitalize_single_letters <- function(words) {
out <- words
out[which(nchar(out) == 1L)] <- toupper(out[which(nchar(out) == 1L)])
out
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.