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

Label composer #465

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 @@ -71,6 +71,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
@@ -1,5 +1,8 @@
# scales (development version)

* 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)

Check warning on line 31 in R/label-compose.R

View check run for this annotation

Codecov / codecov/patch

R/label-compose.R#L31

Added line #L31 was not covered by tests
}
label_list <- lapply(label_list, as_function, call = call)

function(x) {
if (length(x) == 0) {
return(character())

Check warning on line 37 in R/label-compose.R

View check run for this annotation

Codecov / codecov/patch

R/label-compose.R#L37

Added line #L37 was not covered by tests
}
orig <- x
for (labeller in label_list) {
x <- labeller(x)
attr(x, "orig") <- orig
Copy link
Contributor Author

@teunbrand teunbrand Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attaching the original breaks as an attribute here so that 'aware' functions could use original numeric breaks if they've already been converted to character (when you need a number's sign or something)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something more specific than orig? orig_label is at least more descriptive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer orig_breaks then because we attach breaks and not labels as attributes. This confusion just go to show how orig was ambiguous 😅

}
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

- title: Axis breaks
desc: >
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
)

})
Loading