Skip to content

Commit 86c41ac

Browse files
krlmlrhadley
authored andcommitted
Better error message when using +.gg() as unary operator (#2638)
* better error message when using +.gg() as unary operator * tweak wording * add test
1 parent 3f63d62 commit 86c41ac

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

R/plot-construction.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
#' # This can be useful to return from a function.
3939
#' base + list(subset(mpg, fl == "p"), geom_smooth())
4040
"+.gg" <- function(e1, e2) {
41+
if (missing(e2)) {
42+
stop("Cannot use `+.gg()` with a single argument. ",
43+
"Did you accidentally put + on a new line?",
44+
call. = FALSE)
45+
}
46+
4147
# Get the name of what was passed in as e2, and pass along so that it
4248
# can be displayed in error messages
4349
e2name <- deparse(substitute(e2))

tests/testthat/test-error.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
context("error")
2+
3+
test_that("various misuses of +.gg (#2638)", {
4+
expect_error(
5+
{
6+
ggplot(mtcars, aes(hwy, displ))
7+
+ geom_point()
8+
},
9+
"Cannot use `+.gg()` with a single argument. Did you accidentally put + on a new line?",
10+
fixed = TRUE
11+
)
12+
13+
expect_error(
14+
geom_point() + geom_point(),
15+
"Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?",
16+
fixed = TRUE
17+
)
18+
})

0 commit comments

Comments
 (0)