-
Notifications
You must be signed in to change notification settings - Fork 2.1k
aes() should accept !!!
at its first and second argument
#2675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@lionel- can you please take a look/think about this? (The problem is that |
Until we figure this out, there's a work around: aes(,, !!!q) |
This isn't very clean, but I think we can introduce some tweak like this. library(rlang)
aes <- function (x, y, ...) {
x_expr <- quo_get_expr(enquo0(x))
if (startsWith(expr_deparse(x_expr), "!!!")) {
expr <- expr(quos(!!x_expr))
exprs <- eval(expr, caller_env())
} else {
exprs <- enquos(x = x, y = y, ..., .ignore_empty = "all")
}
aes <- ggplot2:::new_aes(exprs, env = parent.frame())
ggplot2:::rename_aes(aes)
}
aes(x, y, colour = Species)
#> Aesthetic mapping:
#> * `x` -> `x`
#> * `y` -> `y`
#> * `colour` -> `Species`
q <- quos(colour = Species)
aes(!!!q)
#> Aesthetic mapping:
#> * `colour` -> `Species` Created on 2022-04-14 by the reprex package (v2.0.1) |
I'm not fluent in {rlang}, but I can improvise on a theme, in this case Yutannihilation's code above. library(rlang)
try_inject <- function(var, name, exprs, env = caller_env()) {
expr <- quo_get_expr(var)
if (is_missing(expr)) {
return(exprs)
}
if (startsWith(expr_deparse(expr), "!!!")) {
expr <- expr(quos(!!expr))
expr <- eval(expr, env)
} else {
expr <- enquos(var, .ignore_empty = "all")
names(expr) <- name
}
c(exprs, expr)
}
aes <- function(x, y, ...) {
exprs <- enquos(..., .ignore_empty = "all")
exprs <- try_inject(enquo0(x), "x", exprs)
exprs <- try_inject(enquo0(y), "y", exprs)
aes <- ggplot2:::new_aes(exprs, env = parent.frame())
ggplot2:::rename_aes(aes)
}
q <- quos(colour = Species)
# New cases that now work
aes(x = Sepal.Width, !!!q)
#> Aesthetic mapping:
#> * `x` -> `Sepal.Width`
#> * `colour` -> `Species`
aes(y = Petal.Width, !!!q)
#> Aesthetic mapping:
#> * `colour` -> `Species`
#> * `y` -> `Petal.Width`
# These already worked with Yutannilation's code
aes(x = Sepal.Width, y = Petal.Width, !!!q)
#> Aesthetic mapping:
#> * `colour` -> `Species`
#> * `x` -> `Sepal.Width`
#> * `y` -> `Petal.Width`
aes(!!!q)
#> Aesthetic mapping:
#> * `colour` -> `Species` Created on 2022-04-15 by the reprex package (v2.0.1) |
Cool. You are already better at using rlang than I! |
Thanks @yutannihilation and @teunbrand! I've sent #4802 based on your implementations. |
Thanks!! |
I feel this is a bit counter-intuitive. Is it possible to introduce some tweak to enable this?
Created on 2018-05-31 by the reprex package (v0.2.0).
The text was updated successfully, but these errors were encountered: