[Rd] group generics

Ross Boylan ross at biostat.ucsf.edu
Mon Nov 30 23:57:08 CET 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



Martin Morgan wrote:
> Hi Ross -- 
> 
> Ross Boylan <ross at biostat.ucsf.edu> writes:
> 
>> I have classes A and B, where B contains A.  In the implementation of
>> the group generic for B I would like to use the corresponding group
>> generic for A.  Is there a way to do that?

>> setMethod("Arith", signature(e1="numeric", e2="B"), function(e1, e2) {
>> 	# the next line does not work right
>>   v <- selectMethod("callGeneric", signature=c("numeric", "A"))(e1, e2)
> 
>     v <- callGeneric(e1, as(e2, "A"))
> 
> or probably
> 
>    v <- callNextMethod(e1, e2)
> 
> Martin

A different error this time, one that looks a lot like the report from
Stephen.Pope at ubs.com on 2007-12-24 concerning callNextMethod:, except
this is with
callGeneric.

HOWEVER, the problem is erratic; when I started from scratch and took
this code into a workspace and executed the commands, they worked as
expected.  I had various false starts and revisions, as well as the real
code on which the example is based, when the error occurred.  I tried
taking in the real code (which defines generics with Arith from my
actual classes, and which also fails as below), and the example still
worked.


My revised code:

setClass("A",
         representation=representation(xa="numeric")
         )

setMethod("Arith", signature(e1="numeric", e2="A"), function(e1, e2) {
  new("A", xa=callGeneric(e1, e2 at xa))
}
         )

setClass("B",
         representation=representation(xb="numeric"),
         contains=c("A")
         )

setMethod("Arith", signature(e1="numeric", e2="B"), function(e1, e2) {
  new("B", xb=e1*e2 at xb, callNextMethod())
}
)

Results:
> options(error=recover)
> tb <- new("B", xb=1:3, new("A", xa=10))
> 3*tb
Error in get(fname, envir = envir) : object '.nextMethod' not found

Enter a frame number, or 0 to exit

 1: 3 * tb
 2: 3 * tb
 3: test.R#16: new("B", xb = e1 * e2 at xb, callNextMethod())
 4: initialize(value, ...)
 5: initialize(value, ...)
 6: callNextMethod()
 7: .nextMethod(e1 = e1, e2 = e2)
 8: test.R#6: new("A", xa = callGeneric(e1, e2 at xa))
 9: initialize(value, ...)
10: initialize(value, ...)
11: callGeneric(e1, e2 at xa)
12: get(fname, envir = envir)

Selection: 0

The callGeneric in frame 11 is trying to get the primitive for
multiplying numeric times numeric.  Quoting from Pope's analysis:
[The primitive...]
> does not get the various "magic" variables such as .Generic, .Method,
> etc. defined in its frame. Thus, callGeneric() fails when, failing to
> find ".Generic" then takes the function symbol for the call (which
> callNextMethod() has constructed to be ".nextMethod") and attempts to
> look it up, which of course also fails, leading to the resulting error
> seen above.

I'm baffled, and hoping someone on the list has an idea.
I'm running R 2.10 under ESS (in particular, I use c-c c-l in the code
file to read in the code) on XP.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAksUTcQACgkQTEwcvZWfjMgEdwCfYt/bmsXG76rq3BpbByBYNjLY
ubsAoKnBnBMbd+OlBL2YOg3vWslL35Zg
=D58x
-----END PGP SIGNATURE-----



More information about the R-devel mailing list