Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New label_glue() function #459

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyLoad: yes
Roxygen: list(markdown = TRUE, r6 = FALSE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# scales (development version)

* New `label_glue()` labelling function for interpolated strings (#457).

# scales 1.3.0

## Better type support
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"))

})
Loading