Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 195
6.2.4 Group Generic Functions
ОглавлениеIt is possible to implement methods for multiple generic functions with one function via the mechanism of group generics. Group generic methods can be defined for four pre-specified groups of functions in R: “Math,” “Ops,” “Summary” and “Complex.”6
The four “group generics” and the functions they include are:
1 Group Math: Members of this group dispatch on x. Most members accept only one argument, except log, round and signif that accept one or two arguments, while trunc accepts one or more. Members of this group are:abs, sign, sqrt, floor, ceiling, trunc, round, signifexp, log, expm1, log1p, cos, sin, tan, cospi, sinpi, tanpi, acos, asin, atan cosh, sinh, tanh, acosh, asinh, atanhlgamma, gamma, digamma, trigammacumsum, cumprod, cummax, cummin
2 Group Ops: This group contains both binary and unary operators ( +, - and !): when a unary operator is encountered, the Ops method is called with one argument and e2 is missing. The classes of both arguments are considered in dispatching any member of this group. For each argument, its vector of classes is examined to see if there is a matching specific (preferred) or Ops method. If a method is found for just one argument or the same method is found for both, it is used. If different methods are found, there is a warning about incompatible methods: in that case or if no method is found for either argument, the internal method is used. If the members of this group are called as functions, any argument names are removed to ensure that positional matching is always used.+, -, *, /, ∧, \%\%, \%/\%&, |, !==, !=, <, <=, >=, >
3 Group Summary: Members of this group dispatch on the first argument supplied.all, anysum, prodmin, maxrange
4 Group complex: Members of this group dispatch on z.Arg, Conj, Im, Mod, Re
Of course, a method defined for an individual member of the group takes precedence over a method defined for the group as a whole, because it is more specific.