Skip to content

Commit

Permalink
Label composer (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand authored Oct 22, 2024
1 parent ea44928 commit 0f3929a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export(col_shift)
export(colour_ramp)
export(comma)
export(comma_format)
export(compose_label)
export(compose_trans)
export(cscale)
export(cut_long_scale)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* `label_log()` has a `signed` argument for displaying negative numbers
(@teunbrand, #421).

* New function `compose_label()` to chain together label formatting functions
(#462)

# scales 1.3.0

## Better type support
Expand Down
48 changes: 48 additions & 0 deletions R/label-compose.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#' Compose two or more label formatters together
#'
#' This labeller provides a general mechanism for composing two or more
#' labellers together.
#'
#' @param ... One or more labelling functions. These will be applied to breaks
#' consecutively.
#' [Lambda syntax][rlang::as_function] is allowed.
#' @param call A call to display in error messages.
#'
#' @return A labelling function that applies the provided
#' functions to breaks to return labels.
#'
#' @export
#'
#' @examples
#' demo_continuous(
#' c(-100, 100),
#' labels = compose_label(abs, number, ~paste0(.x, " foobar"), toupper)
#' )
#'
#' # Same result
#' demo_continuous(
#' c(-100, 100),
#' labels = compose_label(abs, label_number(suffix = " FOOBAR"))
#' )
compose_label <- function(..., call = caller_env()) {

label_list <- list2(...)
if (length(label_list) == 0) {
return(identity)
}
label_list <- lapply(label_list, as_function, call = call)

function(x) {
if (length(x) == 0) {
return(character())
}
orig <- x
for (labeller in label_list) {
x <- labeller(x)
attr(x, "orig_breaks") <- orig
}
x[is.na(orig)] <- NA
names(x) <- names(x) %||% names(orig)
x
}
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ reference:
contents:
- starts_with("label_")
- matches("format")
- compose_label
- number_options

- title: Axis breaks
Expand Down
35 changes: 35 additions & 0 deletions man/compose_label.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-compose.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("compose_labels can chain together functions", {

labeller <- compose_label(`-`, label_number(suffix = " foo"), toupper)
expect_equal(
labeller(c(0.1, 1.0, 10.0)),
c("-0.1 FOO", "-1.0 FOO", "-10.0 FOO"),
ignore_attr = TRUE
)

})

0 comments on commit 0f3929a

Please sign in to comment.