Skip to content
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

Strip .data from default labels #3746

Merged
merged 4 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ fail.
* `stat_summary()` and related functions now support rlang-style lambda functions
(#3568, @dkahle).

* `strip_dots()` strips the data masking pronoun `.data` to make better
hadley marked this conversation as resolved.
Show resolved Hide resolved
axis labels.

* Addition of partial themes to plots has been made more predictable;
stepwise addition of individual partial themes is now equivalent to
addition of multple theme elements at once (@clauswilke, #3039).
Expand Down
21 changes: 14 additions & 7 deletions R/aes-evaluation.r
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ is_staged <- function(x) {
}

# Strip dots from expressions
strip_dots <- function(expr) {
strip_dots <- function(expr, env) {
if (is.atomic(expr)) {
expr
} else if (is.name(expr)) {
Expand All @@ -144,23 +144,30 @@ strip_dots <- function(expr) {
}
} else if (is_quosure(expr)) {
# strip dots from quosure and reconstruct the quosure
expr <- new_quosure(
strip_dots(quo_get_expr(expr)),
new_quosure(
strip_dots(quo_get_expr(expr), env = quo_get_env(expr)),
quo_get_env(expr)
)
} else if (is.call(expr)) {
if (identical(expr[[1]], quote(stat))) {
if (is_call(expr, "$") && is_symbol(expr[[2]], ".data")) {
expr[[3]]
} else if (is_call(expr, "[[") && is_symbol(expr[[2]], ".data")) {
tryCatch(
sym(eval(expr[[3]], env)),
error = function(e) expr[[3]]
)
} else if (is_call(expr, "stat")) {
strip_dots(expr[[2]])
} else {
expr[-1] <- lapply(expr[-1], strip_dots)
expr[-1] <- lapply(expr[-1], strip_dots, env = env)
expr
}
} else if (is.pairlist(expr)) {
# In the unlikely event of an anonymous function
as.pairlist(lapply(expr, strip_dots))
as.pairlist(lapply(expr, strip_dots, env = env))
} else if (is.list(expr)) {
# For list of aesthetics
lapply(expr, strip_dots)
lapply(expr, strip_dots, env = env)
} else {
abort(glue("Unknown input: {class(expr)[1]}"))
}
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-aes-calculated.r
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ test_that("strip_dots remove dots around calculated aesthetics", {
)
})

test_that("strip_dots handles tidy evaluation pronouns", {
expect_identical(strip_dots(aes(.data$x))$x, quo(x))
expect_identical(strip_dots(aes(.data[["x"]]))$x, quo(x))

var <- "y"
f <- function() {
var <- "x"
aes(.data[[var]])$x
}
expect_identical(quo_get_expr(strip_dots(f())), quote(x))
})

test_that("make_labels() deprases mappings properly", {
# calculation stripped from labels
expect_identical(make_labels(aes(x = ..y..)), list(x = "y"))
Expand Down