[R] substitute, eval, quote and functions

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Feb 9 01:30:07 CET 2004


Douglas Bates <bates at stat.wisc.edu> writes:

> "David A. van Leeuwen" <myfirstname at elseware.nl> writes:
> 
> > Hi,
> > 
> > i am working with large data frames with many dependend variables.  I
> > want to write some functions that will allow me to quickly select
> > variables from the frame and plot them in various colors depending on
> > factor columns, possibly selecting rows according to factor
> > conditions. In order to do this in a nice function, i need to
> > understand how to work with a column name in the body of a
> > function. To simplify my problem, how do i write a function with a
> > body like
> > 
> > 
> > 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)



-- 
   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