[R] Method for $

Tim Hesterberg timh at insightful.com
Mon Nov 21 21:49:31 CET 2005


Ulrike Groemping <groemping at tfh-berlin.de> wrote:

>I have defined a class "myclass" and would like the slots to be extractable 
>not only by "@" but also by "$". I now try to write a method for "$" that 
>simply executes the request object at slotname, whenever someone calls 
>object$slotname for any object of class "myclass".
>I don't manage to find out how I can provide this function with "slotname", 
>so that one and the same function works for any arbitrary slotname a user 
>might choose.
>...

I would caution against defining methods for $.

In addition to Martin Maechler and Duncan Temple Lange's warnings
about the danger of this, I would note that it could make R run much
slower.  I once tried defining a method for it in S-PLUS; that
converted $ into a generic function, which slowed down every call to $,
of which there are many.

Even if it wouldn't slow down R, as we work to make R and S-PLUS more
compatible you or someone else might try your code in S-PLUS, and
cause a big speed hit.

>Maybe I could (and should?) have defined the class with just one slot
>that contains the list, which would make it behave like I want it
>immediately.

Why not make it a list with an S3 class, rather than an S4 class?

Tim Hesterberg

========================================================
| Tim Hesterberg       Research Scientist              |
| timh at insightful.com  Insightful Corp.                |
| (206)802-2319        1700 Westlake Ave. N, Suite 500 |
| (206)283-8691 (fax)  Seattle, WA 98109-3012, U.S.A.  |
|                      www.insightful.com/Hesterberg   |
========================================================
Download the S+Resample library from www.insightful.com/downloads/libraries

Two Research Scientist positions:
	data mining
	frailty/mixed effects
    http://www.insightful.com/company/jobs.asp

Speak out about biased science in Washington D.C.
    http://home.comcast.net/~timhesterberg/ScientificIntegrity.html




More information about the R-help mailing list