[R] stumped by eval

Ross Boylan ross at biostat.ucsf.edu
Wed Feb 13 05:52:28 CET 2008

In the following example, the inner evaluation pulls in the global value
of subset (a function) rather than the one I thought I was passing in (a
vector).  Can anyone help me understand what's going on, and what I need
to do to fix the problem?

f0 <- function(formula, data,
                              subset, na.action
  f1(formula, data,
                     subset, na.action

f1 <- function (formula, data,
                              subset, na.action
    mf <- match.call()
    mf[[1]] <- as.name("model.frame")
    mf <- eval(mf, parent.frame())

> netto <- data.frame(Reading=seq(3), Spec=seq(3), Reader=seq(3))
> t3 <- f0(Reading~0+Spec+Reader, netto, c(1))
Error in xj[i] : invalid subscript type 'closure'
> traceback()
7: `[.data.frame`(list(Reading = 1:3, Spec = 1:3, Reader = 1:3), 
       function (x, ...) 
       UseMethod("subset"), , FALSE)
6: model.frame.default(formula = formula, data = data, subset = subset, 
       na.action = na.action)
5: model.frame(formula = formula, data = data, subset = subset, 
       na.action = na.action)
4: eval(expr, envir, enclos)
3: eval(mf, parent.frame())
2: f1(formula, data, subset, na.action)
1: f0(Reading ~ 0 + Spec + Reader, netto, c(1))

I started with a case in which f0 was called with only 2 arguments
(i.e., subset was missing), and that is the case I'm ultimately
interested in.  However, even the situation above is failing.

According to resume, the class of subset is numeric in frames 2 and 4,
but a function in frames 3 and 5.  That may be correct for frame 3,
since it is the evaluation function without a named argument subset (and
thus it picks up the global value).  But it's wrong for 5 (and higher).

eval itself is primitive; I don't understand exactly what is going on
with the 2 entries for it (frames 3 + 4).

R package 2.6.1-1 on Debian GNU/Linux.

More information about the R-help mailing list