[R] setMethod for "["

From: Musa Parmaksiz <musa.parmaksiz_at_gmail.com>
Date: Thu, 20 Mar 2008 23:40:38 +0100


Hi R-Help,

Please consider the following simple case: I have a class like

setClass("myClass",

               representation(x="matrix", y="character"))

and I would like to use the method *"["* for a *myClass* objects (but changing the default *drop* argument from TRUE to FALSE):

setMethod("[","myClass",
function(x,i,j,...,drop=FALSE)
{

    x <- x_at_x
    callNextMethod()
    x<-as.myClass(x)
}
)

suppose that *as.myClass* method has been already defined. Actually, all I want is to pass all the arguments to *"["* method for *matrix*, except changing the default behaviour for *drop*.

When I execute:
> test<-new("myClass",x=cbind(1:3,4:6),y="a")
> test[1,] # works as expected

[1] 1 4

> test[1,drop=TRUE] # does not work

Error: argument "j" is missing, with no default Error in callNextMethod() : error in evaluating a 'primitive' next method

but with a matrix the two cases work:
> m<-cbind(1:3,4:6)
> m[1,]

[1] 1 4
> m[1,drop=TRUE]

[1] 1

Can you please advise me a solution for this problem? Thank you in advance for the help.

Musa

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 20 Mar 2008 - 23:03:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 21 Mar 2008 - 00:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive