Skip to content

when testing if an identifier refers to a calculated aesthetic, check the whole identifier, not a substring #917

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

Merged
merged 4 commits into from
Feb 25, 2014
Merged
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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ ggplot2 0.9.3.1.99
* `ggpcp()`, `ggfluctuation()`, `ggmissing()`, `ggstructure()`, and
`ggorder()` are now defunct and have been removed.

* `aes()` no more treats variables like `a..x..b as a calculated aesthetic.
(@krlmlr, #834.)

* Add `"none"` to documentation of `theme()` for parameter `legend.position`.
(@krlmlr, #829)

Expand Down
2 changes: 1 addition & 1 deletion R/labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ggtitle <- function(label) {
# Convert aesthetic mapping into text labels
make_labels <- function(mapping) {
remove_dots <- function(x) {
gsub("\\.\\.([a-zA-z._]+)\\.\\.", "\\1", x)
gsub(.calculated_aes_regex, "\\1", x)
}

lapply(mapping, function(x) remove_dots(deparse(x)))
Expand Down
10 changes: 6 additions & 4 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,19 @@ Layer <- proto(expr = {
#' @export
layer <- Layer$new

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

# Determine if aesthetic is calculated
is_calculated_aes <- function(aesthetics) {
match <- "\\.\\.([a-zA-z._]+)\\.\\."
stats <- rep(FALSE, length(aesthetics))
grepl(match, sapply(aesthetics, deparse))
grepl(.calculated_aes_regex, sapply(aesthetics, deparse))
}

# Strip dots from expressions
strip_dots <- function(aesthetics) {
match <- "\\.\\.([a-zA-z._]+)\\.\\."
strings <- lapply(aesthetics, deparse)
strings <- lapply(strings, gsub, pattern = match, replacement = "\\1")
strings <- lapply(strings, gsub, pattern = .calculated_aes_regex,
replacement = "\\1")
lapply(strings, function(x) parse(text = x)[[1]])
}
7 changes: 7 additions & 0 deletions inst/tests/test-layer.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
context("Layer")

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")
})