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 } ) 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
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
>>>>> "Ulrike" == Ulrike Gr??mping <groemping at tfh-berlin.de> >>>>> on Fri, 18 Nov 2005 15:37:58 +0100 writes:Ulrike> Dear R experts, Ulrike> I have defined a class "myclass" and would like the Ulrike> slots to be extractable not only by "@" but also by "$". hmm, I know this is not what you've asked for, but why would you want to do that? To me, it seems a pretty harmful idea that only leads to confusion and does not provide sensible functionality. When S4 classes where created, using '@' instead of '@' with a *different* meaning was very much on purpose and I think it's a bit too ambitious to try being smarter than John Chambers, the creator of the S4 object system, on this. Martin Maechler, ETH Zurich Ulrike> I now try to write a method for "$" that Ulrike> simply executes the request object at slotname, whenever someone calls Ulrike> object$slotname for any object of class "myclass". Ulrike> .............................. Ulrike> ..............................
Ulrike Groemping <groemping at tfh-berlin.de> wrote:>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 would caution against defining methods for $. In addition to Martin Maechler and Duncan Temple Lange's warnings about the danger of this, I would note that it could make R run much slower. I once tried defining a method for it in S-PLUS; that converted $ into a generic function, which slowed down every call to $, of which there are many. Even if it wouldn't slow down R, as we work to make R and S-PLUS more compatible you or someone else might try your code in S-PLUS, and cause a big speed hit.>Maybe I could (and should?) have defined the class with just one slot >that contains the list, which would make it behave like I want it >immediately.Why not make it a list with an S3 class, rather than an S4 class? Tim Hesterberg =======================================================| Tim Hesterberg Research Scientist | | timh at insightful.com Insightful Corp. | | (206)802-2319 1700 Westlake Ave. N, Suite 500 | | (206)283-8691 (fax) Seattle, WA 98109-3012, U.S.A. | | www.insightful.com/Hesterberg | =======================================================Download the S+Resample library from www.insightful.com/downloads/libraries Two Research Scientist positions: data mining frailty/mixed effects http://www.insightful.com/company/jobs.asp Speak out about biased science in Washington D.C. http://home.comcast.net/~timhesterberg/ScientificIntegrity.html
Apparently Analagous Threads
- Whine in dual core Windows PC if R uses full CPU capacity
- R CMD check: problems possibly from mapply?
- Bug in str or issue with class management in my package?
- Question regarding lu in package Matrix
- Wishlist: install.packages to look for the newest version of a package (PR#13851)