Permalink
Browse files

Evaluate dcast args in correct environment. Fixes #18

  • Loading branch information...
1 parent d8db5ce commit 562dd64bb03bfb0ba29749af06e1d1203ab45b95 @hadley committed Apr 19, 2012
Showing with 16 additions and 1 deletion.
  1. +2 −0 NEWS
  2. +1 −1 R/cast.r
  3. +13 −0 inst/tests/test-cast.r
View
2 NEWS
@@ -1,3 +1,5 @@
+* Fix evaluation bug revealed by knitr. (Fixes #18)
+
Version 1.2.1
-------------
View
@@ -108,7 +108,7 @@ cast <- function(data, formula, fun.aggregate = NULL, ..., subset = NULL, fill =
# Need to branch here depending on whether or not we have strings or
# expressions - strings should avoid making copies of the data
- vars <- lapply(formula, eval.quoted, envir = data, enclos = parent.frame())
+ vars <- lapply(formula, eval.quoted, envir = data, enclos = parent.frame(2))
# Compute labels and id values
ids <- lapply(vars, id, drop = drop)
View
@@ -162,3 +162,16 @@ test_that("factor value columns are handled", {
expect_true(is.character(c4))
})
+
+test_that("dcast evaluated in correct argument", {
+ g <- c("a", "b")
+ expr <- quote({
+ df <- data.frame(x = letters[1:2], y = letters[1:3], z = rnorm(6))
+ g <- c('b', 'a')
+ dcast(df, y ~ ordered(x, levels = g))
+ })
+
+ res <- eval(expr, envir = new.env())
+ expect_equal(names(res), c("y", "b", "a"))
+
+})

0 comments on commit 562dd64

Please sign in to comment.