Skip to content

Let stage() work without after_stat or after_scale #4890

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

Closed
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
17 changes: 14 additions & 3 deletions R/aes-evaluation.r
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,20 @@ strip_stage <- function(expr) {
if (is_call(uq_expr, c("after_stat", "after_scale"))) {
uq_expr[[2]]
} else if (is_call(uq_expr, "stage")) {
# Prefer stat mapping if present, otherwise original mapping (fallback to
# scale mapping) but there should always be two arguments to stage()
uq_expr$after_stat %||% uq_expr$start %||% (if (is.null(uq_expr$after_scale)) uq_expr[[3]]) %||% uq_expr[[2]]
# Prefer stat mapping if present
# i.e. `b` for `stage(a, after_stat = b)`
if (!is.null(uq_expr$after_stat))
return(uq_expr$after_stat)

# The case when after_stat is positionally provided.
# Be aware that we need to reject `stage(a, after_scale = c)`.
# i.e. `b` for `stage(a, b)`
if (is.null(uq_expr$after_scale) && length(uq_expr) >= 3)
return(uq_expr[[3]])

# The case when stage() has no `after_stat`
# i.e. `stage(a, after_scale = c)` or `stage(a)`
uq_expr[[2]]
} else {
expr
}
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-aes-calculated.r
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,26 @@ test_that("staged aesthetics warn appropriately for duplicated names", {
# One warning in building due to `stage()`/`after_scale()`
expect_snapshot_warning(ggplot_build(p))
})

test_that("stage() works with only the start arg (#4873)", {
do_strip_stage <- function(...) {
args <- enexprs(...)
stage_call <- expr(stage(!!!args))
strip_stage(stage_call)
}

# the standard usage
expect_equal(do_strip_stage(x, after_stat = y), expr(y))

# positional
expect_equal(do_strip_stage(x, y), expr(y))

# when with only the start arg, simply strips stage()
expect_equal(do_strip_stage(x), expr(x))

# when after_scale is specified without after_stat, return the start arg
expect_equal(do_strip_stage(x, after_scale = y), expr(x))

# when an explicit NULL is specified, use it
expect_equal(do_strip_stage(x, after_stat = NULL), expr(NULL))
})