Skip to content

Commit

Permalink
New label_glue() function (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand authored Oct 22, 2024
1 parent f0cd669 commit dd2cf2f
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export(label_currency)
export(label_date)
export(label_date_short)
export(label_dollar)
export(label_glue)
export(label_log)
export(label_math)
export(label_number)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# scales (development version)
* New `label_glue()` labelling function for interpolated strings (#457).
* `fullseq()` and by extension `breaks_width()` can now deal with unsorted
ranges (#435).
* New `label_date_short(leading)` argument to replace leading zeroes (#442)
Expand Down
39 changes: 39 additions & 0 deletions R/label-glue.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' Interpolated labels
#'
#' Use `label_glue()` to perform string interpolation using the \pkg{glue}
#' package. Enclosed expressions will be evaluated as R code.
#'
#' @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
#' character vector of labels.
#' @export
#' @family labels for continuous scales
#' @family labels for discrete scales
#'
#' @examples
#' # Example variables
#' animal <- "penguin"
#' species <- c("Adelie", "Chinstrap", "Emperor", "Gentoo")
#'
#' # Typical use, note that {x} will become the breaks
#' demo_discrete(species, labels = label_glue("The {x}\n{animal}"))
#' # It adapts to the breaks that are present
#' 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}", ..., parse = FALSE, .envir = caller_env()) {
args <- list2(...)
force_all(pattern, parse, .envir)
function(x) {
x <- inject(glue::glue_data(list(x = x), pattern, !!!args, .envir = .envir))
if (parse) {
x <- parse_safe(x)
}
x
}
}
1 change: 1 addition & 0 deletions man/label_bytes.Rd

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

1 change: 1 addition & 0 deletions man/label_currency.Rd

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

58 changes: 58 additions & 0 deletions man/label_glue.Rd

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

1 change: 1 addition & 0 deletions man/label_number_auto.Rd

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

1 change: 1 addition & 0 deletions man/label_number_si.Rd

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

1 change: 1 addition & 0 deletions man/label_ordinal.Rd

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

2 changes: 2 additions & 0 deletions man/label_parse.Rd

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

1 change: 1 addition & 0 deletions man/label_percent.Rd

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

1 change: 1 addition & 0 deletions man/label_pvalue.Rd

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

1 change: 1 addition & 0 deletions man/label_scientific.Rd

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

1 change: 1 addition & 0 deletions man/label_wrap.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-label-glue.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("label_glue environments work out as intended", {

x <- LETTERS[1:3]
y <- "foo"

# Note `{x}` should mask the `x <- LETTERS[1:3]` above
f <- label_glue("{x} and {y}")
expect_equal(f(letters[1:3]), c("a and foo", "b and foo", "c and foo"))

})

0 comments on commit dd2cf2f

Please sign in to comment.