Skip to content

fix detection if an aesthetic is calculated #935

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
wants to merge 2 commits into from
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
7 changes: 3 additions & 4 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,17 @@ Layer <- proto(expr = {
layer <- Layer$new

# Regex to determine if an identifier refers to a calculated aesthetic
.calculated_aes_regex <- "^\\.\\.([a-zA-z._]+)\\.\\.$"
.calculated_aes_regex <- "(?:^|(?<![a-zA-Z._]))[.][.]([a-zA-Z._]+)[.][.](?:$|(?![a-zA-Z._]))"

# Determine if aesthetic is calculated
is_calculated_aes <- function(aesthetics) {
stats <- rep(FALSE, length(aesthetics))
grepl(.calculated_aes_regex, sapply(aesthetics, deparse))
grepl(.calculated_aes_regex, sapply(aesthetics, deparse), perl = TRUE)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since aesthetics is a call/name, might be better to do a little computing on the language here, rather than using a complicated regexp.


# Strip dots from expressions
strip_dots <- function(aesthetics) {
strings <- lapply(aesthetics, deparse)
strings <- lapply(strings, gsub, pattern = .calculated_aes_regex,
replacement = "\\1")
replacement = "\\1", perl = TRUE)
lapply(strings, function(x) parse(text = x)[[1]])
}
3 changes: 3 additions & 0 deletions inst/tests/test-layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ test_that("Correctly decide if a variable is a calculated aesthetic", {
expect_true(is_calculated_aes(aes(x=..density..)))
expect_false(is_calculated_aes(aes(x=a..x..b)))
expect_equal(as.character(strip_dots(aes(x=..density..))), "density")
expect_true(is_calculated_aes(aes(x=mean(..density..))))
expect_false(is_calculated_aes(aes(x=mean(a..x..b))))
expect_equal(as.character(strip_dots(aes(x=mean(..density..)))), "mean(density)")
})