Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Evaluate aes() in calling environment. Fixes #743 #744

Closed
wants to merge 2 commits into from

4 participants

@wch
Collaborator

This fixes #743.

@hadley
Owner

Does this also fix #248?

@wch
Collaborator
wch commented

It partially fixes #248. Each aes() is evaluated in the scope of the ggplot call, not in the scope where each aes() is called.

@antoine-lizee

It doesn't seem that it is fixed. If I run the code below...
I may have forgot something, but this is very sad because it makes it difficult to use ggplot2 in a function!

s1 <- 2

test <- function(){
   s1 <- 4
   TEST <- data.frame(num=c(1,2,3,4), name=c("a","b","c","d"))
   print(with(TEST, num/s1))
   print(qplot(data = TEST, x=name, y=num/s1))
   print(ggplot(data = TEST) + geom_bar( aes(x=name, y=num/s1) , stat= 'identity'))
}
@hadley
Owner

Could you please rebase/merge against master, re-document with the development version of roxygen2 (install_github("klutometis/roxygen) and resubmit?

@hadley hadley closed this
@tsibley

@hadley In the interest of seeing this merged, I rebased and re-documented as PR #982.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 2 deletions.
  1. +1 −1  R/plot.r
  2. +26 −1 inst/tests/test-aes.r
View
2  R/plot.r
@@ -93,7 +93,7 @@ is.ggplot <- function(x) inherits(x, "ggplot")
#' @seealso \url{http://had.co.nz/ggplot2}
#' @method ggplot data.frame
#' @export
-ggplot.data.frame <- function(data, mapping=aes(), ..., environment = globalenv()) {
+ggplot.data.frame <- function(data, mapping=aes(), ..., environment = parent.frame()) {
if (!missing(mapping) && !inherits(mapping, "uneval")) stop("Mapping should be created with aes or aes_string")
p <- structure(list(
View
27 inst/tests/test-aes.r
@@ -59,4 +59,29 @@ test_that("function aes_auto", {
structure(list(x = bquote(xp), y = bquote(yp)), class = "uneval"))
expect_equal(aes_auto(df), structure(setNames(list(), character(0)), class = "uneval"))
})
-
+
+test_that("aes evaluation environment", {
+
+ # Accessing an undefined variable should result in error
+ p <- ggplot(mtcars, aes(x = wt + foo, y = mpg)) + geom_point()
+ expect_error(ggplot_build(p))
+
+
+ # When variable is defined, there should be no error
+ foo <- 4
+ # This is just a roundabout way of testing for no error, since there's no
+ # function like expect_no_error.
+ expect_true(is.ggplot(ggplot_build(p)$plot))
+
+
+ # Calling from a function with a variable defined in that function shouldn't
+ # result in an error because aes() should be evaluated in the scope of the
+ # function.
+ f <- function() {
+ foo2 <- 4
+ ggplot(mtcars, aes(x = wt + foo2, y = mpg)) +
+ geom_point()
+ }
+ expect_true(is.ggplot(ggplot_build(f())$plot))
+
+})
Something went wrong with that request. Please try again.