[R] evaluating variables in the context of a data frame

Zack Weinberg zackw at panix.com
Fri Jun 8 20:45:18 CEST 2007


On 6/8/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> > After a bit more experimentation I figured out that this does what I want:
> >
> >> h <- function(x, d) eval(substitute(x), d, parent.frame())
> >
> > but I don't understand why the substitute() helps, or indeed why it
> > has any effect at all...
>
> Within the evaluation frame of h, x is a promise to evaluate an
> expression.  substitute(x) extracts the expression.  If you just use x,
> it gets evaluated in the frame from which h was called, rather than in a
> frame created from d.

Thanks, that's helpful.  Could you comment on substitute() use in the
message I just posted which contains the actual code I'm trying to get
to work?  In addition to the question asked there, after your
explanation I still do not understand why

  g <- ...
  xyplot ( ..., groups=g, ... )

should refuse to find g, and the same thing with groups=substitute(g)
works (well, gets farther before blowing up).

zw



More information about the R-help mailing list