[R] how to eval subset?

Ott-Siim Toomet ott.toomet at ut.ee
Sat Feb 10 07:11:21 CET 2007


Hi,

I have problems with subset when calling a function from inside a function
from inside a function.  Here is a small example to be called as 'f1()'.
'eval()' in f3 fails with error message

Error in eval(expr, envir, enclos) : object "subs" not found

Is it possible to supplement subset to data, to be calculated in a
different environment than the data itself?

Thanks in advance
Ott

The example follows:
----------------------------------------------------------
N <- 10
f1 <- function() {
   x <- runif(N)
   y <- runif(N)
   f2(y ~ x)
}

f2 <- function(formula, data=sys.frame(sys.parent())) {
   subs <- rep(c(TRUE, FALSE), length.out=N)
   f3(formula, data=data, subset=subs)
}

f3 <- function(formula, data=sys.frame(sys.parent()), subset) {
   mf <- match.call()
   m <- match(c("formula", "subset", "data"), names(mf), 0)
   mf <- mf[c(1, m)]
   mf[[1]] <- as.name("model.frame")
   eval(data)
# anyone able to explain me why this eval is necessary?
# can it be done in a smarter way?
   mf <- eval(mf, envir=parent.frame())
# finds the data environment, but not environment for subset
}



More information about the R-help mailing list