Skip to content

Commit 32ccdb2

Browse files
committed
convert labels to S7
1 parent 7fce100 commit 32ccdb2

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ S3method(ggplot_add,Scale)
6464
S3method(ggplot_add,by)
6565
S3method(ggplot_add,data.frame)
6666
S3method(ggplot_add,default)
67-
S3method(ggplot_add,labels)
6867
S3method(ggplot_add,list)
6968
S3method(ggplot_build,ggplot)
7069
S3method(ggplot_build,ggplot_built)

R/labels.R

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,24 @@ setup_plot_labels <- function(plot, layers, data) {
175175
#' p +
176176
#' labs(title = "title") +
177177
#' labs(title = NULL)
178-
labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
179-
tag = waiver(), dictionary = waiver(), alt = waiver(),
180-
alt_insight = waiver()) {
181-
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
182-
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
183-
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
184-
dictionary = dictionary, .ignore_empty = "all")
178+
labs <- S7::new_class(
179+
"labels", parent = S7::new_S3_class("gg"),
180+
constructor = function(..., title = waiver(), subtitle = waiver(),
181+
caption = waiver(), tag = waiver(), dictionary = waiver(),
182+
alt = waiver(), alt_insight = waiver()) {
183+
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
184+
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
185+
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
186+
dictionary = dictionary, .ignore_empty = "all")
185187

186-
is_waive <- vapply(args, is.waiver, logical(1))
187-
args <- args[!is_waive]
188-
# remove duplicated arguments
189-
args <- args[!duplicated(names(args))]
190-
args <- rename_aes(args)
191-
192-
structure(args, class = c("labels", "gg"))
193-
}
188+
is_waive <- vapply(args, is.waiver, logical(1))
189+
args <- args[!is_waive]
190+
# remove duplicated arguments
191+
args <- args[!duplicated(names(args))]
192+
args <- rename_aes(args)
193+
S7::new_object(args)
194+
}
195+
)
194196

195197
#' @rdname labs
196198
#' @export

R/plot-construction.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ ggplot_add.Scale <- function(object, plot, object_name) {
139139
plot$scales$add(object)
140140
plot
141141
}
142-
#' @export
143-
ggplot_add.labels <- function(object, plot, object_name) {
142+
S7::method(ggplot_add, labs) <- function(object, plot, object_name) {
144143
update_labels(plot, object)
145144
}
146145
#' @export

tests/testthat/test-labels.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ test_that("setting guide labels works", {
2525
expect_identical(labs(color = "my label")$colour, "my label")
2626

2727
# No extra elements exists
28-
expect_equal(labs(title = "my title"), list(title = "my title"), ignore_attr = TRUE) # formal argument
29-
expect_equal(labs(colour = "my label"), list(colour = "my label"), ignore_attr = TRUE) # dot
30-
expect_equal(labs(foo = "bar"), list(foo = "bar"), ignore_attr = TRUE) # non-existent param
28+
expect_length(labs(title = "my title"), 1) # formal argument
29+
expect_length(labs(colour = "my label"), 1) # dot
30+
expect_length(labs(foo = "bar"), 1) # non-existent param
3131

3232
# labs() has list-splicing semantics
3333
params <- list(title = "my title", tag = "A)")
3434
expect_identical(labs(!!!params)$tag, "A)")
3535

3636
# NULL is preserved
37-
expect_equal(labs(title = NULL), list(title = NULL), ignore_attr = TRUE)
37+
expect_length(labs(title = NULL), 1)
3838

3939
# ggtitle works in the same way as labs()
4040
expect_identical(ggtitle("my title")$title, "my title")

0 commit comments

Comments
 (0)