Movatterモバイル変換


[0]ホーム

URL:


S4groupGeneric {methods}R Documentation

S4 Group Generic Functions

Description

Methods can be defined forgroup generic functions. Each groupgeneric function has a number ofmember generic functionsassociated with it.

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

The functions shown in this documentation page all reside in themethods package, but the mechanism is available to anyprogrammer, by callingsetGroupGeneric (provided packagemethods is attached).

Usage

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

Arguments

x,y,z,e1,e2

objects.

digits

number of digits to be used inround orsignif.

...

further arguments passed to or from methods.

na.rm

logical: should missing values be removed?

Details

Methods can be defined for the group generic functions by calls tosetMethod in the usual way.Note that the group generic functionsshould never be called directly– a suitable error message will result if they are. When metadatafor a group generic is loaded, the methods defined become methodsfor the members of the group, but only if no method has beenspecified directly for the member function for the same signature.The effect is that group generic definitions are selected beforeinherited methods but after directly specified methods. For more onmethod selection, seeMethods_Details.

There are alsoS3 groupsMath,Ops,Summary,ComplexandmatrixOps, see?S3groupGeneric,with no correspondingR objects, but these are irrelevant for S4group generic functions.

The members of the group defined by a particular generic can beobtained by callinggetGroupMembers. For the groupgeneric functions currently defined in this package the members areas follows:

Arith

"+","-","*","^","%%","%/%","/"

Compare

"==",">","<","!=","<=",">="

Logic

"&","|".

Ops

"Arith","Compare","Logic"

Math

"abs","sign","sqrt","ceiling","floor","trunc","cummax","cummin","cumprod","cumsum","log","log10","log2","log1p","acos","acosh","asin","asinh","atan","atanh","exp","expm1","cos","cosh","cospi","sin","sinh","sinpi","tan","tanh","tanpi","gamma","lgamma","digamma","trigamma"

Math2

"round","signif"

Summary

"max","min","range","prod","sum","any","all"

Complex

"Arg","Conj","Im","Mod","Re"

matrixOps

"%*%"

Note thatOps merely consists of three sub groups.

All the functions in these groups (other than the group genericsthemselves) are basic functions inR. They are not by default S4 genericfunctions, and many of them are defined as primitives. However, you can still defineformal methods for them, both individually and via the group generics. It all works more or less as youmight expect, admittedly via a bit of trickery in the background.SeeMethods_Details for details.

Note that two members of theMath group,log andtrunc, have ... as an extra formal argument.Since methods forMath will have only one formal argument,you must set a specific method for these functions in order to callthem with the extra argument(s).

For further details about group generic functions see section 10.5 ofthe second reference.

References

Chambers, John M. (2016)Extending R,Chapman & Hall. (Chapters 9 and 10.)

Chambers, John M. (2008)Software for Data Analysis: Programming with RSpringer. (Section 10.5)

See Also

The functioncallGeneric is nearly alwaysrelevant when writing a method for a group generic. See theexamples below and in section 10.5 ofSoftware for Data Analysis.

SeeS3groupGeneric for S3 group generics.

Examples

setClass("testComplex", slots = c(zz = "complex"))## method for whole group "Complex"getGroupMembers("Complex") # "Arg"  "Conj" "Im"   "Mod"  "Re"  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+2iz2 <- new("testComplex", zz = z1)stopifnot(identical(Mod(z2), c("groupMethod", Mod(z1))))stopifnot(identical(Arg(z2), c("ArgMethod", Arg(z1))))selectMethod("Re", signature = "testComplex") # shows Generic: .. "Re" & .."Complex"

[Packagemethods version 4.6.0Index]

[8]ページ先頭

©2009-2025 Movatter.jp