-
Notifications
You must be signed in to change notification settings - Fork 109
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
Label composer #465
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f2cd0e7
label composer
teunbrand 2f652ea
document
teunbrand 004feb7
add test
teunbrand 396f1dd
add news bullet
teunbrand 6865923
add to pkgdown
teunbrand 7d9cb6f
fix merge conflict
teunbrand 5287a43
rename attribute for original breaks
teunbrand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") <- orig | ||
} | ||
x[is.na(orig)] <- NA | ||
names(x) <- names(x) %||% names(orig) | ||
x | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
|
||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 descriptiveThere was a problem hiding this comment.
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 howorig
was ambiguous 😅