What does 'Can't use `!!!` at top level.' mean and how to resolve it?
Apparently this is a known issue of aes()
as you can verify here. A workaround is this:
b <- function(data, x, y, ...) {
x <- rlang::enquo(x)
y <- rlang::enquo(y)
dots <- rlang::enquos(...)
segment_args <- dots[names(dots) != "size"]
ggplot(data, aes(!!x, !!y)) +
geom_segment(aes(, y = 0, xend = !!x, yend = !!y, !!!segment_args)) +
geom_point(aes(, , !!!dots))
}
Notice the single comma in geom_segment()
and the double comma in geom_point()
.
If you follow the instructions of rlang, you get some further details:
> rlang::last_error()
<error>
message: Can't use `!!!` at top level.
class: `rlang_error`
backtrace:
1. global::b(d, cyl, n, color = factor(am), size = am)
4. ggplot2::aes(y = 0, xend = !!x, yend = !!y, !!!segment_args)
5. rlang::enquos(x = x, y = y, ..., .ignore_empty = "all")
6. rlang:::endots(...)
7. rlang:::map(...)
8. base::lapply(.x, .f, ...)
9. rlang:::FUN(X[[i]], ...)
Call `rlang::last_trace()` to see the full backtrace
Then
> rlang::last_trace()
█
1. └─global::b(d, cyl, n, color = factor(am), size = am)
2. ├─ggplot2::geom_segment(aes(y = 0, xend = !!x, yend = !!y, !!!segment_args))
3. │ └─ggplot2::layer(...)
4. └─ggplot2::aes(y = 0, xend = !!x, yend = !!y, !!!segment_args)
5. └─rlang::enquos(x = x, y = y, ..., .ignore_empty = "all")
6. └─rlang:::endots(...)
7. └─rlang:::map(...)
8. └─base::lapply(.x, .f, ...)
9. └─rlang:::FUN(X[[i]], ...)
So it appears the issue is with !!!segment_args
EDIT 1: just playing around but since segment_args is currently a single value, I tried the following and the error indeed disappears:
b <- function(data, x, y, ...) {
x <- rlang::enquo(x)
y <- rlang::enquo(y)
dots <- rlang::enquos(...)
print(dots)
segment_args <- dots[[setdiff(names(dots), "size")]]
print(names(dots))
print(segment_args)
ggplot(data, aes(!!x, !!y)) +
geom_segment(aes(y = 0, xend = !!x, yend = !!y, !!segment_args)) +
geom_point(aes(!!!dots))
}
This only confirms that the issue is with the usage of !!! since the above gives now an error for aes(!!!dots)
instead and it depends on the fact that in the example there is only one element in segment_args, but it may give a handhold for further investigation
I don't think you need to quote / unquote anymore. Instead, you can use the double bracket {{ x }}
and leave the dots as dots ...
The following works and is much easier to understand:
b <- function(data, x, y, ...) {
ggplot(data, aes( {{x}} , {{y}} )) +
geom_segment(aes(y = 0, xend = {{x}}, yend = {{y}}, ...)) +
geom_point(aes(...))
}