[R] Environment of a formula

Adrian Baddeley adrian at maths.uwa.edu.au
Mon Feb 7 04:56:43 CET 2005


Wise and merciful R-helpers:

I want to equip a data frame with an attribute 
which specifies how to plot some of the columns. 

Up to now we have been doing this by giving the data frame 
a `formula' attribute, that can be passed to plot.formula.

For example
      dat <- data.frame(x=1:100,y=runif(100),z=100:1)
      attr(dat, "plotme") <- (z ~ x)
      ......
      ......
      if(missing(desiredformula))
	desiredformula <- attr(dat, "plotme")
      plot(desiredformula, data=dat)

We just got bitten by the fact that a formula object has a `.Environment'
attribute, which may be huge, depending on the environment
in which the formula was created. In the example above there is
no upper limit on the size of the object 'dat' !!!!
That is, environment(attr(dat, "plotme")) could be huge.

What is the recommended/safe way to avoid this?
I don't need the formula to have an environment at all; I'm just using
the formula structure to represent the format of the plot.

It appears that we can't set the environment to NULL;
should we set it to the Global environment e.g. using as.formula?

Or is it wiser to save the formula as a character string 
in the object 'dat' and only convert it back to a formula 
at the last possible moment?

thanks

Adrian Baddeley




More information about the R-help mailing list