Berton, thanks for your code, it does work on my problem. However, I do want the $ do work the extraction, and it does if I do it right (see answer from Duncan Temple Lang). Regards, Ulrike
---------- Original Message ----------- From: Berton Gunter <[EMAIL PROTECTED]> To: "'Uwe Ligges'" <[EMAIL PROTECTED]>, "'Ulrike Grömping'" <[EMAIL PROTECTED]> Cc: <r-help@stat.math.ethz.ch> Sent: Fri, 18 Nov 2005 09:42:12 -0800 Subject: RE: [R] Method for $ > I believe that a recommended S4 convention is simply to write methods for > slot extraction with the same name as the slot. Thus, for an object,x of > class 'myclass' > > slotname(x) > > would give [EMAIL PROTECTED] This is a more natural interface than either @ > or $ > (and I am not sure that methods for $ can be defined due to S3/S4 class > consistency details). > > Obviously, it is trivial, but a bit tedious, to manually write such methods > for any class you care to. However, I wrote a small generator function that > automatically generates the slot extraction methods for any class if you > wish to do things this way. I just run this right after I've define a new > class,'myclass', to generate slot access methods by: slotget('myclass') > > The code is below. I would appreciate feedback on any errors or suggestions > for improvement. > > Cheers, > > -- Bert Gunter > Genentech Non-Clinical Statistics > South San Francisco, CA > > slotGet<-function(x,where=topenv()) > #automatically generates accessor and replacement methods for class x > { > if(!isClass(x,where=where))stop(paste('Class',x,'not found')) > slots<-slotNames(getClass(x,where=where)) > for(slt in slots){ > ## slot accessor > fun<-function(object)NULL > if(!isGeneric(slt,where=where)){ > if(exists(slt,where=where,mode="function")) > fun<-get(slt,pos=where,mode="function") > else body(fun)<-substitute(standardGeneric(slt)) > setGeneric(slt,fun,where=where) > } > if(!existsMethod(slt,x,where=where)){ > body(fun)<-substitute([EMAIL PROTECTED]) > setMethod(slt,x,fun,where=where) > } > } > invisible() > } > > > -----Original Message----- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On Behalf Of Uwe Ligges > > Sent: Friday, November 18, 2005 8:46 AM > > To: Ulrike Grömping > > Cc: r-help@stat.math.ethz.ch > > Subject: Re: [R] Method for $ > > > > Ulrike Grömping wrote: > > > > > Dear R experts, > > > > > > 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 [EMAIL PROTECTED], 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 have tried > > > > > > setMethod("$", signature(x="myclass"), function(x,slotname){ > > > [EMAIL PROTECTED] > > > } > > > ) > > > > > > Ulrike, > > > > what about (untested!): > > slot(x, slotname) > > > > Best wishes from Dortmund and Lena, > > Uwe Ligges > > > > > > > This produced the error message: > > > In method for function "$": expanding the signature to > > > include omitted arguments in definition: name = "missing" > > > Error in rematchDefinition(definition, fdef, mnames, > > fnames, signature) : > > > methods can add arguments to the generic only if > > '...' is an argument > > > to the generic > > > > > > My searches for a solution of this problem have not been > > successful. Can > > > someone help? > > > > > > Thanks and regards, > > > Ulrike Grömping, Berlin > > > > > > ______________________________________________ > > > R-help@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 > > > > ______________________________________________ > > R-help@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 > > ------- End of Original Message ------- ______________________________________________ R-help@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