[R] substitute, eval, quote and functions

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Feb 9 10:25:54 CET 2004


Peter Dalgaard <p.dalgaard at biostat.ku.dk> writes:

> > > scatter.plot <- function (data, x, y) {
> > >     plot(data$x, data$y)
> > > }
> > 
> > Use 
> >    plot(data[[x]], data[[y]])
> > instead
> 
> The plot labels won't come out right then. I think David was
> looking for something like 
> 
> function(data, x, y)
>         eval(substitute(plot(x,y)), data)
> 
> or to be able to pass names:
> 
> function(data, x, y)
>    eval(substitute(plot(x,y),list(x=as.name(x), y=as.name(y))), data)

...On the other hand, come to think of it, there's quite a lot to be
said for just doing

   plot(data[[x]], data[[y]], xlab=x, ylab=y) 

at least in the latter case. (The former has the advantage that you
can pass in entire expressions.)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list