[R] S4 group generic with additional argument

Andrew Whyte andrew.whyte at cantab.net
Thu Jun 30 10:57:59 CEST 2011




Dear R-help,

I am attempting to set the "Summary" group generic function
for my S4 class. Using the standard arguments, this is easy. The function
returns the result of the generic for every element in a list:



  setClass("TestClass", representation(elements =
"list"))

  tc <- new("TestClass", elements = list(a = 1:5, b =
6:20))



  setMethod("Summary", "TestClass", function(x,
..., na.rm)

      sapply(x at elements, .Generic, ..., na.rm =
na.rm))

  max(tc) # c(a = 5, b = 20)

 

However if I try to add a new argument ("k" in this example)
I am unable to access the ".Generic" function. (Note: R restart
required to reload the group generic definition):

  setMethod("Summary", "TestClass",
function(x, k, ..., na.rm)

        k * sapply(x at elements,
.Generic, ..., na.rm = na.rm))

  max(tc, -1)  # Error in match.fun(FUN) : object '.Generic' not
found

The help for ?setMethod covers this briefly with "Roughly, if the
generic has ... as one of its arguments, then the method may have extra
formal arguments, which will be matched from the arguments matching ... in
the call to ‘f’." Am I misunderstanding / misusing
it?

I see the same results on

    R version 2.12.1 (2010-12-16)

    Platform: i486-pc-linux-gnu (32-bit)

and

    R version 2.13.0 (2011-04-13)

    Platform: i386-pc-mingw32/i386 (32-bit)

Many thanks,

Andrew



More information about the R-help mailing list