Skip to content

Commit 6d6a076

Browse files
authored
Strip .data from default labels (#3746)
1 parent ae61f7f commit 6d6a076

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ fail.
162162
* `stat_summary()` and related functions now support rlang-style lambda functions
163163
(#3568, @dkahle).
164164

165+
* The data mask pronoun, `.data`, is now stripped from default labels.
166+
165167
* Addition of partial themes to plots has been made more predictable;
166168
stepwise addition of individual partial themes is now equivalent to
167169
addition of multple theme elements at once (@clauswilke, #3039).

R/aes-evaluation.r

Lines changed: 14 additions & 7 deletions
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) {
135+
strip_dots <- function(expr, env) {
136136
if (is.atomic(expr)) {
137137
expr
138138
} else if (is.name(expr)) {
@@ -144,23 +144,30 @@ strip_dots <- function(expr) {
144144
}
145145
} else if (is_quosure(expr)) {
146146
# strip dots from quosure and reconstruct the quosure
147-
expr <- new_quosure(
148-
strip_dots(quo_get_expr(expr)),
147+
new_quosure(
148+
strip_dots(quo_get_expr(expr), env = quo_get_env(expr)),
149149
quo_get_env(expr)
150150
)
151151
} else if (is.call(expr)) {
152-
if (identical(expr[[1]], quote(stat))) {
152+
if (is_call(expr, "$") && is_symbol(expr[[2]], ".data")) {
153+
expr[[3]]
154+
} else if (is_call(expr, "[[") && is_symbol(expr[[2]], ".data")) {
155+
tryCatch(
156+
sym(eval(expr[[3]], env)),
157+
error = function(e) expr[[3]]
158+
)
159+
} else if (is_call(expr, "stat")) {
153160
strip_dots(expr[[2]])
154161
} else {
155-
expr[-1] <- lapply(expr[-1], strip_dots)
162+
expr[-1] <- lapply(expr[-1], strip_dots, env = env)
156163
expr
157164
}
158165
} else if (is.pairlist(expr)) {
159166
# In the unlikely event of an anonymous function
160-
as.pairlist(lapply(expr, strip_dots))
167+
as.pairlist(lapply(expr, strip_dots, env = env))
161168
} else if (is.list(expr)) {
162169
# For list of aesthetics
163-
lapply(expr, strip_dots)
170+
lapply(expr, strip_dots, env = env)
164171
} else {
165172
abort(glue("Unknown input: {class(expr)[1]}"))
166173
}

tests/testthat/test-aes-calculated.r

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ test_that("strip_dots remove dots around calculated aesthetics", {
2424
)
2525
})
2626

27+
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))
30+
31+
var <- "y"
32+
f <- function() {
33+
var <- "x"
34+
aes(.data[[var]])$x
35+
}
36+
expect_identical(quo_get_expr(strip_dots(f())), quote(x))
37+
})
38+
2739
test_that("make_labels() deprases mappings properly", {
2840
# calculation stripped from labels
2941
expect_identical(make_labels(aes(x = ..y..)), list(x = "y"))

0 commit comments

Comments
 (0)