Skip to content

Commit 4f51f51

Browse files
authored
Avoid stripping .data pronoun during evaluation (#3850)
1 parent 382176d commit 4f51f51

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

R/aes-evaluation.r

+10-10
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ is_staged <- function(x) {
132132
}
133133

134134
# Strip dots from expressions
135-
strip_dots <- function(expr, env) {
135+
strip_dots <- function(expr, env, strip_pronoun = FALSE) {
136136
if (is.atomic(expr)) {
137137
expr
138138
} else if (is.name(expr)) {
@@ -145,29 +145,29 @@ strip_dots <- function(expr, env) {
145145
} else if (is_quosure(expr)) {
146146
# strip dots from quosure and reconstruct the quosure
147147
new_quosure(
148-
strip_dots(quo_get_expr(expr), env = quo_get_env(expr)),
148+
strip_dots(quo_get_expr(expr), env = quo_get_env(expr), strip_pronoun = strip_pronoun),
149149
quo_get_env(expr)
150150
)
151151
} else if (is.call(expr)) {
152-
if (is_call(expr, "$") && is_symbol(expr[[2]], ".data")) {
153-
expr[[3]]
154-
} else if (is_call(expr, "[[") && is_symbol(expr[[2]], ".data")) {
152+
if (strip_pronoun && is_call(expr, "$") && is_symbol(expr[[2]], ".data")) {
153+
strip_dots(expr[[3]], env, strip_pronoun = strip_pronoun)
154+
} else if (strip_pronoun && is_call(expr, "[[") && is_symbol(expr[[2]], ".data")) {
155155
tryCatch(
156156
sym(eval(expr[[3]], env)),
157157
error = function(e) expr[[3]]
158158
)
159159
} else if (is_call(expr, "stat")) {
160-
strip_dots(expr[[2]])
160+
strip_dots(expr[[2]], env, strip_pronoun = strip_pronoun)
161161
} else {
162-
expr[-1] <- lapply(expr[-1], strip_dots, env = env)
162+
expr[-1] <- lapply(expr[-1], strip_dots, env = env, strip_pronoun = strip_pronoun)
163163
expr
164164
}
165165
} else if (is.pairlist(expr)) {
166166
# In the unlikely event of an anonymous function
167-
as.pairlist(lapply(expr, strip_dots, env = env))
167+
as.pairlist(lapply(expr, strip_dots, env = env, strip_pronoun = strip_pronoun))
168168
} else if (is.list(expr)) {
169169
# For list of aesthetics
170-
lapply(expr, strip_dots, env = env)
170+
lapply(expr, strip_dots, env = env, strip_pronoun = strip_pronoun)
171171
} else {
172172
abort(glue("Unknown input: {class(expr)[1]}"))
173173
}
@@ -194,7 +194,7 @@ make_labels <- function(mapping) {
194194
return(aesthetic)
195195
}
196196
mapping <- strip_stage(mapping)
197-
mapping <- strip_dots(mapping)
197+
mapping <- strip_dots(mapping, strip_pronoun = TRUE)
198198
if (is_quosure(mapping) && quo_is_symbol(mapping)) {
199199
name <- as_string(quo_get_expr(mapping))
200200
} else {

tests/testthat/test-aes-calculated.r

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ test_that("strip_dots remove dots around calculated aesthetics", {
2525
})
2626

2727
test_that("strip_dots handles tidy evaluation pronouns", {
28-
expect_identical(strip_dots(aes(.data$x))$x, quo(x))
29-
expect_identical(strip_dots(aes(.data[["x"]]))$x, quo(x))
28+
expect_identical(strip_dots(aes(.data$x), strip_pronoun = TRUE)$x, quo(x))
29+
expect_identical(strip_dots(aes(.data[["x"]]), strip_pronoun = TRUE)$x, quo(x))
3030

3131
var <- "y"
3232
f <- function() {
3333
var <- "x"
3434
aes(.data[[var]])$x
3535
}
36-
expect_identical(quo_get_expr(strip_dots(f())), quote(x))
36+
expect_identical(quo_get_expr(strip_dots(f(), strip_pronoun = TRUE)), quote(x))
3737
})
3838

3939
test_that("make_labels() deprases mappings properly", {

0 commit comments

Comments
 (0)