Last active
January 11, 2022 12:45
-
-
Save Torvaney/7121f227de151a29d38f2f665863200c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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