Skip to content

Commit b9d289f

Browse files
committed
simplify stage masking
1 parent 842e6be commit b9d289f

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

R/aes-evaluation.R

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,26 @@ from_theme <- function(x) {
206206
x
207207
}
208208

209+
get_stage <- local({
210+
stage <- "start"
211+
function() {stage}
212+
})
213+
214+
set_stage <- function(new_stage) {
215+
old_stage <- environment(get_stage)$stage
216+
environment(get_stage)$stage <- new_stage
217+
invisible(old_stage)
218+
}
219+
209220
#' @rdname aes_eval
210221
#' @export
211222
stage <- function(start = NULL, after_stat = NULL, after_scale = NULL) {
212-
start
213-
}
214-
stage_calculated <- function(start = NULL, after_stat = NULL, after_scale = NULL) {
215-
after_stat
216-
}
217-
stage_scaled <- function(start = NULL, after_stat = NULL, after_scale = NULL) {
218-
after_scale
223+
switch(
224+
get_stage(),
225+
after_stat = after_stat,
226+
after_scale = after_scale,
227+
start
228+
)
219229
}
220230

221231
# Regex to determine if an identifier refers to a calculated aesthetic

R/geom-.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,9 @@ Geom <- ggproto("Geom",
157157
# This order means that they will have access to all default aesthetics
158158
if (length(modifiers) != 0) {
159159
# Set up evaluation environment
160-
env <- child_env(baseenv(), after_scale = after_scale)
161-
# Mask stage with stage_scaled so it returns the correct expression
162-
stage_mask <- child_env(emptyenv(), stage = stage_scaled)
163-
mask <- new_data_mask(as_environment(data, stage_mask), stage_mask)
164-
mask$.data <- as_data_pronoun(mask)
165-
modified_aes <- lapply(substitute_aes(modifiers), eval_tidy, mask, env)
160+
set_stage("after_scale")
161+
env <- child_env(baseenv())
162+
modified_aes <- lapply(substitute_aes(modifiers), eval_tidy, data, env)
166163

167164
# Check that all output are valid data
168165
nondata_modified <- check_nondata_cols(modified_aes)

R/layer.R

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
#' `NA`, the default, includes if any aesthetics are mapped.
5959
#' `FALSE` never includes, and `TRUE` always includes.
6060
#' It can also be a named logical vector to finely select the aesthetics to
61-
#' display. To include legend keys for all levels, even
62-
#' when no data exists, use `TRUE`. If `NA`, all levels are shown in legend,
61+
#' display. To include legend keys for all levels, even
62+
#' when no data exists, use `TRUE`. If `NA`, all levels are shown in legend,
6363
#' but unobserved levels are omitted.
6464
#' @param inherit.aes If `FALSE`, overrides the default aesthetics,
6565
#' rather than combining with them. This is most useful for helper functions
@@ -303,6 +303,7 @@ Layer <- ggproto("Layer", NULL,
303303
}
304304

305305
# Evaluate aesthetics
306+
set_stage("start")
306307
env <- child_env(baseenv(), stage = stage)
307308
evaled <- lapply(aesthetics, eval_tidy, data = data, env = env)
308309
evaled <- compact(evaled)
@@ -386,13 +387,9 @@ Layer <- ggproto("Layer", NULL,
386387
data_orig <- plot$scales$backtransform_df(data)
387388

388389
# Add map stat output to aesthetics
389-
env <- child_env(baseenv(), stat = stat, after_stat = after_stat)
390-
stage_mask <- child_env(emptyenv(), stage = stage_calculated)
391-
mask <- new_data_mask(as_environment(data_orig, stage_mask), stage_mask)
392-
mask$.data <- as_data_pronoun(mask)
393-
390+
set_stage("after_stat")
394391
new <- substitute_aes(new)
395-
stat_data <- lapply(new, eval_tidy, mask, env)
392+
stat_data <- lapply(new, eval_tidy, data_orig, env)
396393

397394
# Check that all columns in aesthetic stats are valid data
398395
nondata_stat_cols <- check_nondata_cols(stat_data)

0 commit comments

Comments
 (0)