Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0f3929a

Browse files
authored
Label composer (#465)
1 parentea44928 commit0f3929a

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

‎NAMESPACE‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export(col_shift)
7474
export(colour_ramp)
7575
export(comma)
7676
export(comma_format)
77+
export(compose_label)
7778
export(compose_trans)
7879
export(cscale)
7980
export(cut_long_scale)

‎NEWS.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*`label_log()` has a`signed` argument for displaying negative numbers
1212
(@teunbrand,#421).
1313

14+
* New function`compose_label()` to chain together label formatting functions
15+
(#462)
16+
1417
#scales 1.3.0
1518

1619
##Better type support

‎R/label-compose.R‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#' Compose two or more label formatters together
2+
#'
3+
#' This labeller provides a general mechanism for composing two or more
4+
#' labellers together.
5+
#'
6+
#' @param ... One or more labelling functions. These will be applied to breaks
7+
#' consecutively.
8+
#' [Lambda syntax][rlang::as_function] is allowed.
9+
#' @param call A call to display in error messages.
10+
#'
11+
#' @return A labelling function that applies the provided
12+
#' functions to breaks to return labels.
13+
#'
14+
#' @export
15+
#'
16+
#' @examples
17+
#' demo_continuous(
18+
#' c(-100, 100),
19+
#' labels = compose_label(abs, number, ~paste0(.x, " foobar"), toupper)
20+
#' )
21+
#'
22+
#' # Same result
23+
#' demo_continuous(
24+
#' c(-100, 100),
25+
#' labels = compose_label(abs, label_number(suffix = " FOOBAR"))
26+
#' )
27+
compose_label<-function(...,call= caller_env()) {
28+
29+
label_list<- list2(...)
30+
if (length(label_list)==0) {
31+
return(identity)
32+
}
33+
label_list<- lapply(label_list,as_function,call=call)
34+
35+
function(x) {
36+
if (length(x)==0) {
37+
return(character())
38+
}
39+
orig<-x
40+
for (labellerinlabel_list) {
41+
x<- labeller(x)
42+
attr(x,"orig_breaks")<-orig
43+
}
44+
x[is.na(orig)]<-NA
45+
names(x)<- names(x) %||% names(orig)
46+
x
47+
}
48+
}

‎_pkgdown.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ reference:
1919
contents:
2020
-starts_with("label_")
2121
-matches("format")
22+
-compose_label
2223
-number_options
2324

2425
-title:Axis breaks

‎man/compose_label.Rd‎

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
test_that("compose_labels can chain together functions", {
2+
3+
labeller<- compose_label(`-`, label_number(suffix=" foo"),toupper)
4+
expect_equal(
5+
labeller(c(0.1,1.0,10.0)),
6+
c("-0.1 FOO","-1.0 FOO","-10.0 FOO"),
7+
ignore_attr=TRUE
8+
)
9+
10+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp