S4groupGeneric            package:methods            R Documentation

_G_r_o_u_p _G_e_n_e_r_i_c _F_u_n_c_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     Methods can be defined  for _group generic functions_.  Each group
     generic function has a number of _member_ generic functions
     associated with it.

     Methods  defined for a group generic function cause the same
     method to be defined for each member of the group, but a method
     explicitly defined for a  member of the group takes precedence
     over a method defined, with the same signature, for the group
     generic.

     The functions shown in this documentation page all reside in the
     'methods' package, but the mechanism is available to any
     programmer, by calling 'setGroupGeneric'.

_U_s_a_g_e:

     ## S4 group generics:
     Arith(e1, e2)
     Compare(e1, e2)
     Ops(e1, e2)
     Logic(e1, e2)
     Math(x)
     Math2(x, digits)
     Summary(x, ..., na.rm = FALSE)
     Complex(z)

_A_r_g_u_m_e_n_t_s:

x, z, e1, e2: objects.

  digits: number of digits to be used in 'round' or 'signif'.

     ...: further arguments passed to or from methods.

   na.rm: logical: should missing values be removed?

_D_e_t_a_i_l_s:

     Methods can be defined for the group generic functions by calls to
     'setMethod' in the usual way. Note that the group generic
     functions should never be called directly - a suitable error
     message will result if they are.  When metadata for a group
     generic is loaded, the methods defined become methods for the
     members of the group, but only if no method has been specified
     directly for the member function for the same signature. The
     effect is that group generic definitions are selected before
     inherited methods but after directly specified methods.  For more
     on method selection, see 'Methods'.

     There are also S3 groups 'Math', 'Ops', 'Summary' and 'Complex',
     see '?S3groupGeneric', with no corresponding R objects, but these
     are irrelevant for S4 group generic functions.

     The members of the group defined by a particular generic can be
     obtained by calling 'getGroupMembers'. For the group generic
     functions currently defined in this package the members are as
     follows:

     '_A_r_i_t_h' '"+"', '"-"', '"*"', '"^"', '"%%"', '"%/%"', '"/"'

     '_C_o_m_p_a_r_e' '"=="', '">"', '"<"', '"!="', '"<="', '">="'

     '_L_o_g_i_c' '"&"', '"|"'.

     '_O_p_s' '"Arith"', '"Compare"', '"Logic"'

     '_M_a_t_h' '"abs"', '"sign"', '"sqrt"', '"ceiling"', '"floor"',
          '"trunc"', '"cummax"', '"cummin"', '"cumprod"', '"cumsum"',
          '"log"', '"log10"', '"log2"', '"log1p"', '"acos"', '"acosh"',
          '"asin"', '"asinh"', '"atan"', '"atanh"', '"exp"', '"expm1"',
          '"cos"', '"cosh"', '"sin"', '"sinh"', '"tan"', '"tanh"',
          '"gamma"', '"lgamma"', '"digamma"', '"trigamma"'

     '_M_a_t_h_2' '"round"', '"signif"'

     '_S_u_m_m_a_r_y' '"max"', '"min"', '"range"', '"prod"', '"sum"', '"any"',
          '"all"'

     '_C_o_m_p_l_e_x' '"Arg"', '"Conj"', '"Im"', '"Mod"', '"Re"'

     Note that 'Ops' merely consists of three sub groups.

     All the functions in these groups (other than the group generics
     themselves) are basic functions in R.  They are not by default S4
     generic functions, and many of them are defined as primitives. 
     However, you can still define formal methods for them, both
     individually and via the group generics.  It all works more or
     less as you might expect, admittedly via a bit of trickery in the
     background. See Methods for details.

     Note that two members of the 'Math' group, 'log' and 'trunc', have
     ... as an extra formal argument. Since methods for 'Math' will
     have only one formal argument, you must set a specific method for
     these functions in order to call them with the extra argument(s).

     For further details about group generic functions see section 10.5
     of _Software for Data Analysis_.

_R_e_f_e_r_e_n_c_e_s:

     Chambers, John M. (2008) _Software for Data Analysis: Programming
     with R_ Springer.  (For the R version.)

     Chambers, John M. (1998) _Programming with Data_ Springer (For the
     original S4 version).

_S_e_e _A_l_s_o:

     The function 'callGeneric' is nearly always relevant when writing
     a method for a group generic.  See the examples below and in
     section 10.5 of _Software for Data Analysis_.

     See S3groupGeneric for S3 group generics.

_E_x_a_m_p_l_e_s:

     setClass("testComplex", representation(zz = "complex"))
     ## method for whole group "Complex"
     setMethod("Complex", "testComplex",
               function(z) c("groupMethod", callGeneric(z@zz)))
     ## exception for Arg() :
     setMethod("Arg", "testComplex",
               function(z) c("ArgMethod", Arg(z@zz)))
     z1 <- 1+2i
     z2 <- new("testComplex", zz = z1)
     stopifnot(identical(Mod(z2), c("groupMethod", Mod(z1))))
     stopifnot(identical(Arg(z2), c("ArgMethod", Arg(z1))))

