[R] Method for $
Duncan Temple Lang
duncan at wald.ucdavis.edu
Fri Nov 18 19:26:51 CET 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ulrike Grömping wrote:
> Uwe,
>
> thank thank you very much for your suggestion. This does in fact work, if I
> define
>
> "$.myclass"<-function(x,slotname){slot(x,slotname)}
>
> which solves my immediate need.
>
> However, the setMethod version
> setMethod("$", signature(x="myclass"), function(x,slotname){
> slot(x,slotname)
> }
> )
> still produces the same error message as before (cf. below). If anyone has a
> solution for this, I would appreciate it.
>
For the record, the problem you are experiencing is that
you are not using the same argument names in your
method as the function "$" uses. Hence there is a mismatch.
Use
setMethod("$", signature(x="myclass"), function(x, name){
slot(x, name)
} )
i.e. rather than slotname as the second argument, use name
as that is what $ uses.
But, as Martin pointed out, this is not an advisable thing
to do.
And if you want to use an S4 class to house sub-elements,
either explicitly specify their types with
setClass('myclass', representation( a = "integer",
b = "matrix", ....)
or if you want them to be arbitary objects, then think
about why you are using S4.
> Uwe, my best wishes also to Dortmund and Lena, and by the way, it's your book
> that got me started with understanding (part of) the programming aspects of R
> relatively fast.
>
> Regards,
> Ulrike Grömping
>
> ---------- Original Message -----------
> From: Uwe Ligges <ligges at statistik.uni-dortmund.de>
> To: Ulrike Grömping <groemp at tfh-berlin.de>
> Cc: r-help at stat.math.ethz.ch
> Sent: Fri, 18 Nov 2005 17:45:40 +0100
> 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
> ------- End of Original Message -------
>
> ______________________________________________
> 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
- --
Duncan Temple Lang duncan at wald.ucdavis.edu
Department of Statistics work: (530) 752-4782
371 Kerr Hall fax: (530) 752-7099
One Shields Ave.
University of California at Davis
Davis, CA 95616, USA
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFDfhzr9p/Jzwa2QP4RAnoPAKCAeNsDpbAC52kfJnmaBdLeGPUTgwCfTqBF
Ffg0eMA//XcfzY3kjBqGJ8c=
=iO7y
-----END PGP SIGNATURE-----
More information about the R-help
mailing list