Re: [Rd] callNextMethod fails with "$" generic

From: John Chambers <jmc4_at_stanford.edu>
Date: Wed, 10 Nov 2010 16:10:26 -0800

The problem here is that the primitive for `$` does not use standard R evaluation on its second argument, so when it is selected as the next method the call is effectively x$name regardless of the original call.

If possible, I would avoid such cascaded calls of methods for `$`, precisely because of the nonstandard evaluation, which makes the interpretation of each level of the call ambiguous.

If this kind of code is needed, then the primitive method should get a call in which the literal name is in place and the object has been coerced suitably.

In this example, something like

 > setMethod("$", "mylist", function(x, name) {

+     theName <- substitute(name)
+     expr <- substitute(xx$NAME, list(NAME = theName))
+     xx <- unclass(x)
+     eval(expr)
+ })

[1] "$"
 > tl <- new("mylist")
 > tl[["z"]] <- 1
 > tl$z

[1] 1

On 11/10/10 4:49 AM, Vitalie S. wrote:
> Dear Developers,
>
> callNextMethods does not work with "$"
>
> setClass("mylist", contains = "list"):
> setMethod("$",
> signature(x = "mylist"),
> function (x, name){
> cat("here:\n")
> callNextMethod()
> })
>
> tl<- new("mylist")
> tl[["x"]]<- 343
>
> tl$x
> #here:
> #NULL
>
>
> If I use callNextMethod(x=x, name=name)
>
> this error is issued:
>
> Error in function (classes, fdef, mtable) :
> unable to find an inherited method for function "addNextMethod", for signature "function"
>
> It must be something "$" specific. If the above is an expected behavior , how
> should I call next method for "$" generic?
>
> My info:
> R version 2.12.0 Patched (2010-11-01 r53513)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> Same behavior for official R 2.12.0.
>
> Thanks,
> Vitalie.
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 11 Nov 2010 - 00:16:19 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 Thu 11 Nov 2010 - 20:20:19 GMT.

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

list of date sections of archive