R-alpha: terms class

Ross Ihaka (ihaka@stat.auckland.ac.nz)
Tue, 19 Nov 1996 16:48:10 +1300 (NZDT)


From: Ross Ihaka <ihaka@stat.auckland.ac.nz>
Date: Tue, 19 Nov 1996 16:48:10 +1300 (NZDT)
Message-Id: <199611190348.QAA18961@stat13.stat.auckland.ac.nz>
To: Thomas Lumley <thomas@biostat.washington.edu>
Subject: R-alpha: terms class
In-Reply-To: <Pine.SUN.3.91.961118084655.2558B-100000@chekov>
	<Pine.SUN.3.91.961118084655.2558B-100000@chekov>

Thomas Lumley writes:
 > 
 > In S the output of terms() has class "terms".  I tried to add the "terms" 
 > class in R and found two problems that I don't understand.
 > 
 > >tt<-terms(y~x)
 > >class(tt)<-c("terms","formula")
 > > class(tt)
 > [1] "terms"   "formula"
 > > attr(tt,"intercept")<-1
 > > class(tt)
 > [1] "formula"
 > > 
 > 
 > Also, when I defined a "[.terms" method it was ignored, though a 
 > "[.formula" was called if it was defined. For some reason R seems to be 
 > very determined to treat terms objects as formulas.

An the prize for the bug-of-the year goes to ...

I was initially baffled by this one, but I think I am beginning to see
what is going on.  Deep down inside the C code for the terms function
the following lines occur

	CAR(a) = mkString("terms");
        TAG(a) = install("class");
        OBJECT(ans) = 1;

So in fact the object being returned by "terms" does have class
"terms".  So how can the following possibly happen?

	> class(terms(y~a+b))
	[1] "formula"

The answer is that the basic object being returned by "terms" is the
unevaluated expression "y~a+b" with a few attributes attached.  When
"class" is applied to this value, the expression "y~a+b" is
re-evaluated yielding an object of class formula.

I would guess that this is why S returns its value wrapped in an
expression() call.

We will need to do something similar.  I will try to fix this.

My brain hurts.
	Ross
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- For info or help, send "info" or "help",
To [un]subscribe, send "[un]subscribe"
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-