Skip to content

Instantly share code, notes, and snippets.

@Torvaney
Last active January 11, 2022 12:45
Show Gist options
  • Save Torvaney/7121f227de151a29d38f2f665863200c to your computer and use it in GitHub Desktop.
Save Torvaney/7121f227de151a29d38f2f665863200c to your computer and use it in GitHub Desktop.
#' Helper functions for styling with ggtext
#'
#' @param text The text which should be styled
#' @param ... Style options. For example `color="red"`
#' @param tag HTML tag to apply to the text. Defaults to `span`
#'
#' @export
tagged_text <- function(text, ..., tag = "span") {
glue::glue("<{tag} style='{parse_style_options(...)}'>{text}</{tag}>")
}
#' @export
#' @rdname tagged_text
bold <- purrr::partial(tagged_text, tag = "b")
#' @export
#' @rdname tagged_text
italic <- purrr::partial(tagged_text, tag = "i")
parse_style_options <- function(...) {
options <- list(...)
if (length(options) == 0) return("")
options %>%
purrr::imap_chr(function(value, key) {
glue::glue("{key}: \"{value}\"")
}) %>%
glue::glue_collapse(sep = "; ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment