Skip to content

Support splicing in named arguments of aes() #4802

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

Merged
merged 3 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ggplot2 (development version)

* `aes()` now supports the `!!!` operator in its first two arguments
(#2675). Thanks to @yutannihilation and @teunbrand for draft
implementations.

* Require rlang >= 1.0.0 (@billybarc, #4797)

* `geom_violin()` no longer issues "collapsing to unique 'x' values" warning
Expand Down
40 changes: 38 additions & 2 deletions R/aes.r
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,21 @@ NULL
#' cut3 <- function(x) cut_number(x, 3)
#' scatter_by(mtcars, cut3(disp), drat)
aes <- function(x, y, ...) {
exprs <- enquos(x = x, y = y, ..., .ignore_empty = "all")
aes <- new_aes(exprs, env = parent.frame())
xs <- arg_enquos("x")
ys <- arg_enquos("y")
dots <- enquos(...)

args <- c(xs, ys, dots)
args <- Filter(Negate(quo_is_missing), args)

# Pass arguments to helper dummy to throw an error when duplicate
# `x` and `y` arguments are passed through dots
local({
aes <- function(x, y, ...) NULL
inject(aes(!!!args))
})

aes <- new_aes(args, env = parent.frame())
rename_aes(aes)
}

Expand Down Expand Up @@ -426,3 +439,26 @@ extract_target_is_likely_data <- function(x, data, env) {
identical(data_eval, data)
}, error = function(err) FALSE)
}

# Takes a quosure and returns a named list of quosures, expanding
# `!!!` expressions as needed
arg_enquos <- function(name, frame = caller_env()) {
# First start with `enquo0()` which does not process injection
# operators
quo <- inject(enquo0(!!sym(name)), frame)
expr <- quo_get_expr(quo)

if (!is_missing(expr) && is_triple_bang(expr)) {
# Evaluate `!!!` operand and create a list of quosures
env <- quo_get_env(quo)
xs <- eval_bare(expr[[2]][[2]][[2]], env)
xs <- lapply(xs, as_quosure, env = env)
} else {
# Redefuse `x` to process injection operators, then store in a
# length-1 list of quosures
quo <- inject(enquo(!!sym(name)), frame)
xs <- set_names(list(quo), name)
}

new_quosures(xs)
}
22 changes: 22 additions & 0 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,25 @@ split_with_index <- function(x, f, n = max(f)) {
attributes(f) <- list(levels = as.character(seq_len(n)), class = "factor")
unname(split(x, f))
}

is_bang <- function(x) {
is_call(x, "!", n = 1)
}

is_triple_bang <- function(x) {
if (!is_bang(x)) {
return(FALSE)
}

x <- x[[2]]
if (!is_bang(x)) {
return(FALSE)
}

x <- x[[2]]
if (!is_bang(x)) {
return(FALSE)
}

TRUE
}
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/aes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# aes() supports `!!!` in named arguments (#2675)

Code
(expect_error(aes(y = 1, !!!list(y = 2))))
Output
<simpleError in aes(y = 1, y = 2): formal argument "y" matched by multiple actual arguments>

16 changes: 16 additions & 0 deletions tests/testthat/test-aes.r
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ test_that("Warnings are issued when plots use discouraged extract usage within a
expect_warning(ggplot_build(p), "Use of `df\\$x` is discouraged")
})

test_that("aes() supports `!!!` in named arguments (#2675)", {
expect_equal(
aes(!!!list(y = 1)),
aes(y = 1)
)
expect_equal(
aes(!!!list(x = 1), !!!list(y = 2)),
aes(x = 1, y = 2)
)
expect_equal(
aes(, , !!!list(y = 1)),
aes(y = 1)
)
expect_snapshot((expect_error(aes(y = 1, !!!list(y = 2)))))
})

# Visual tests ------------------------------------------------------------

test_that("aesthetics are drawn correctly", {
Expand Down