Skip to content

Pass-through of unevaluated arguments

2 messages · Frank E Harrell Jr, Peter Dalgaard

#
Thanks to Deepayan Sarkar (lattice) and Paul Murrell (grid)
I have nearly finished porting the Hmisc library's xYplot
function to R.  xYplot extends xyplot to allow for error bars,
bands, and a few other features.  In S-Plus I extended the
Trellis function xyplot mainly by writing panel.xYplot.
In R, xyplot contains a lot of code, so I implemented xYplot
by calling xyplot from it.  Things are working quite well but
I would appreciate advice on programming the most elegant
solution to passing unevaluated arguments.

Here's the situation.  xYplot needs to fiddle with
arguments formula and data (which I default to 
sys.frame(sys.parent()) which I believe will pass through
to xyplot OK).  There are several arguments passed
through to xyplot which were the "..." arguments to xYplot.
I want to defer evaluation of these to inside xyplot where
it will evaluate the arguments with respect to the
"data" argument.  A good example is the groups= variable:

xYplot(formula, data=d, groups=g)

where g might be in d or in the frame that invoked
xYplot.  Using for example do.call("xyplot", list(...))
from within xYplot will result in "g not defined".
I can do something like
 
 if(!missing(groups)) groups <- eval(substitute(groups),data)

inside xYplot but I feel there is a more elegant approach
where I would not even have to isolate groups as a separate
argument, that experienced R programmers already know.  I
would appreciate some ideas.

Thanks  -Frank
#
fharrell at virginia.edu writes:
I think 

eval(substitute(xyplot(...)), data, environment(formula))

will get you there if xYplot is function(formula,data,...).
environment(formula) is somewhat preferable to parent.frame() in
general, although they're usually the same thing. 

The tricky bit is when you want to mix external arguments with
something computed inside xYplot. We just had this aired on R-core in
a similar context, and I *think* you get by with substituting in local
variables (notice that R's substitute() is not quite identical to
S's).