[R] Method for $
Ulrike Grömping
groemping at tfh-berlin.de
Fri Nov 18 20:07:56 CET 2005
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 <gunter.berton at gene.com>
To: "'Uwe Ligges'" <ligges at statistik.uni-dortmund.de>, "'Ulrike Grömping'"
<groemp at tfh-berlin.de>
Cc: <r-help at 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 x at slotname. 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(object at slt)
> setMethod(slt,x,fun,where=where)
> }
> }
> invisible()
> }
>
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Uwe Ligges
> > Sent: Friday, November 18, 2005 8:46 AM
> > To: Ulrike Grömping
> > Cc: r-help at 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 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 have tried
> > >
> > > setMethod("$", signature(x="myclass"), function(x,slotname){
> > > x at slotname
> > > }
> > > )
> >
> >
> > 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 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
> >
> > ______________________________________________
> > 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
> >
------- End of Original Message -------
More information about the R-help
mailing list