diff --git a/R/label-glue.R b/R/label-glue.R index 94765ca3..156d4be9 100644 --- a/R/label-glue.R +++ b/R/label-glue.R @@ -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 @@ -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 } } diff --git a/man/label_glue.Rd b/man/label_glue.Rd index ed10e3f8..3b88d0ca 100644 --- a/man/label_glue.Rd +++ b/man/label_glue.Rd @@ -4,7 +4,7 @@ \alias{label_glue} \title{Interpolated labels} \usage{ -label_glue(pattern = "{x}", ..., .envir = caller_env()) +label_glue(pattern = "{x}", ..., parse = FALSE, .envir = caller_env()) } \arguments{ \item{pattern}{A glue string used for formatting. The \code{x} variable holds the @@ -12,6 +12,8 @@ breaks, so that \code{"{x}"} (default) returns the breaks as-is.} \item{...}{Arguments passed on to \code{\link[glue:glue]{glue::glue()}}.} +\item{parse}{Whether to return labels as expressions.} + \item{.envir}{[\code{environment}: \code{parent.frame()}]\cr Environment to evaluate each expression in. Expressions are evaluated from left to right. If \code{.x} is an environment, the expressions are evaluated in that environment and \code{.envir} is ignored. If \code{NULL} is passed, it is equivalent to \code{\link[=emptyenv]{emptyenv()}}.}