Skip to content

Commit 79a9d4e

Browse files
committed
apply to ggproto() and deduplicate members
1 parent 7fd46b1 commit 79a9d4e

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

R/ggproto.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ ggproto <- function(`_class` = NULL, `_inherit` = NULL, ...) {
6868
e <- new.env(parent = emptyenv())
6969

7070
members <- list2(...)
71-
if (length(members) != sum(nzchar(names(members)))) {
72-
cli::cli_abort("All members of a {.cls ggproto} object must be named.")
73-
}
71+
check_named(members, I("Members of a {.cls ggproto} object"))
7472

7573
# R <3.1.2 will error when list2env() is given an empty list, so we need to
7674
# check length. https://github.com/tidyverse/ggplot2/issues/1444

R/stat-bindot.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ StatBindot <- ggproto("StatBindot", Stat,
66
required_aes = "x",
77
non_missing_aes = "weight",
88
default_aes = aes(y = after_stat(count)),
9-
dropped_aes = c("bin", "bincenter"), # these are temporary variables that are created and then removed by the stat
9+
10+
# these are temporary variables that are created and then removed by the stat
11+
dropped_aes = c("weight", "bin", "bincenter"),
1012

1113
setup_params = function(data, params) {
1214
if (is.null(params$binwidth)) {
@@ -122,9 +124,7 @@ StatBindot <- ggproto("StatBindot", Stat,
122124
data$x <- midline
123125
}
124126
return(data)
125-
},
126-
127-
dropped_aes = c("weight", "bin", "bincenter")
127+
}
128128
)
129129

130130

tests/testthat/_snaps/ggproto.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# construction checks input
22

3-
All members of a <ggproto> object must be named.
3+
Members of a <ggproto> object must have names.
44

55
---
66

7-
All members of a <ggproto> object must be named.
7+
Members of a <ggproto> object must have names.
88

99
---
1010

1111
`_inherit` must be a <ggproto> object, not a <data.frame> object.
1212

13+
---
14+
15+
Members of a <ggproto> object cannot have duplicate names ("foo").
16+
1317
# ggproto objects print well
1418

1519
Code

tests/testthat/test-ggproto.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ test_that("construction checks input", {
99
expect_snapshot_error(ggproto("Test", NULL, function(self, a) a))
1010
expect_snapshot_error(ggproto("Test", NULL, a <- function(self, a) a))
1111
expect_snapshot_error(ggproto("Test", mtcars, a = function(self, a) a))
12+
# Duplicate names
13+
expect_snapshot_error(ggproto("Test", NULL, foo = 20, foo = "A"))
1214
})
1315

1416
test_that("all ggproto methods start with `{` (#6459)", {

0 commit comments

Comments
 (0)