[R] How to define S4 methods for '['

McGehee, Robert Robert.McGehee at geodecapital.com
Mon Jun 20 22:46:47 CEST 2005


setMethod("[","foo",function(x,i, j,
.,drop=TRUE)callGeneric(x at dat,i,j,drop=drop) )
                                    ^^^
You have a typo. Use ... instead of . (note ?"[").

Best,
Robert

Robert McGehee
Quantitative Analyst
Geode Capital Management, LLC
53 State Street, 5th Floor | Boston, MA | 02109
Tel: 617/392-8396    Fax:617/476-6389
mailto:robert.mcgehee at geodecapital.com


-----Original Message-----
From: Berton Gunter [mailto:gunter.berton at gene.com] 
Sent: Monday, June 20, 2005 4:25 PM
To: r-help at stat.math.ethz.ch
Subject: [R] How to define S4 methods for '['


Folks:

This is a question about the S4 formal class system.

Suppose I have a class, 'foo', defined by:

setClass('foo',representation(dat='matrix', id='character') )

I wish to define a '[' method for foo that will extract from the 'dat'
slot.
I would have thought that the following would work, but it doesn't:

setMethod("[","foo",function(x,i, j, .,drop=TRUE)callGeneric(x at dat,i,
j,drop=drop) )

The only way I have succeeded in defining this method is using brute
force
eval(parse(. :

{eval(parse(text=paste('.dat(x)[',
        ifelse(missing(i),',','i,'),
        ifelse(missing(j),']','j]'))))
        }

This works. However, I am not able under any circumstances to pass the
drop
argument -- it is ignored.

I would appreciate any pointers about how to do this properly. If  this
is
explicitly in the Green Book (I do not have it with me at the moment),
that
will suffice.

Thanks.

Bert Gunter


	[[alternative HTML version deleted]]

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list