Skip to content

Commit

Permalink
add option to parse
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Oct 11, 2024
1 parent 317b4a4 commit 5595940
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 8 additions & 3 deletions R/label-glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param pattern A glue string used for formatting. The `x` variable holds the
#' breaks, so that `"{x}"` (default) returns the breaks as-is.
#' @param ... Arguments passed on to [`glue::glue()`].
#' @param parse Whether to return labels as expressions.
#' @inheritParams glue::glue
#'
#' @return A labeller function that takes a vector of breaks and returns a
Expand All @@ -25,10 +26,14 @@
#' demo_discrete(species[-3], labels = label_glue("The {x}\n{animal}"))
#' # Contrary to directly glueing species + animal, which results in mislabelling!
#' demo_discrete(species[-3], labels = glue::glue("The {species}\n{animal}"))
label_glue <- function(pattern = "{x}", ..., .envir = caller_env()) {
label_glue <- function(pattern = "{x}", ..., parse = FALSE, .envir = caller_env()) {
args <- list2(...)
force_all(pattern, .envir)
force_all(pattern, parse, .envir)
function(x) {
inject(glue::glue_data(list(x = x), pattern, !!!args, .envir = .envir))
x <- inject(glue::glue_data(list(x = x), pattern, !!!args, .envir = .envir))
if (parse) {
x <- parse_safe(x)
}
x
}
}
4 changes: 3 additions & 1 deletion man/label_glue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5595940

Please sign in to comment.