Rev 51393 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/Methods.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2010 R Core Development Team% Distributed under GPL 2 or later\name{Methods}\alias{Methods}\title{General Information on Methods}\description{This documentation section covers some general topics on how methodswork and how the \pkg{methods} package interacts with the rest of \R. Theinformation is usually not needed to get started with methods andclasses, but may be helpful for moderately ambitious projects, or whensomething doesn't work as expected.The section \dQuote{How Methods Work} describes the underlyingmechanism; \dQuote{S3 Methods} gives the rules applied when S4classes and methods interact with older S3 methods; \dQuote{Method Selection and Dispatch} provides moredetails on how class definitions determine which methods are used;\dQuote{Generic Functions} discusses generic functions as objects.For additional information specifically about class definitions, see \code{\link{Classes}}.}\section{How Methods Work}{A generic function has associated with it acollection of other functions (the methods), all of which have the sameformal arguments as the generic. See the \dQuote{GenericFunctions} section below for more on generic functions themselves.Each \R package will include methods metadata objectscorresponding to each generic function for which methods have beendefined in that package.When the package is loaded into an \R session, the methods for eachgeneric function are \emph{cached}, that is, stored in theenvironment of the generic function along with the methods frompreviously loaded packages. This merged table of methods is used todispatch or select methods from the generic, using class inheritanceand possibly group generic functions (see\code{\link{GroupGenericFunctions}}) to find an applicable method.See the \dQuote{Method Selection and Dispatch} section below.The caching computations ensure that only one version of eachgeneric function is visible globally; although different attachedpackages may contain a copy of the generic function, these behaveidentically with respect to method selection.In contrast, it is possible for the same function name to refer tomore than one generic function, when these have different\code{package} slots. In the latter case, \R considers thefunctions unrelated: A generic function is defined by thecombination of name and package. See the \dQuote{Generic Functions}section below.The methods for a generic are stored according to thecorresponding \code{signature} in the call to \code{\link{setMethod}}that defined the method. The signature associates oneclass name with each of a subset of the formal arguments to thegeneric function. Which formal arguments are available, and theorder in which they appear, are determined by the \code{"signature"}slot of the generic function itself. By default, the signature of thegeneric consists of all the formal arguments except \dots, in theorder they appear in the function definition.Trailing arguments in the signature of the generic will be \emph{inactive} if nomethod has yet been specified that included those arguments in its signature.Inactive arguments are not needed or used in labeling the cachedmethods. (The distinction does not change which methods aredispatched, but ignoring inactive arguments improves theefficiency of dispatch.)All arguments in the signature of the generic function will be evaluated when thefunction is called, rather than using the traditional lazyevaluation rules of S. Therefore, it's important to \emph{exclude}from the signature any arguments that need to be dealt withsymbolically (such as the first argument to function\code{\link{substitute}}). Note that only actual arguments areevaluated, not default expressions.A missing argument enters into the method selection as class\code{"missing"}.The cached methods are stored in anenvironment object. The names used for assignment are aconcatenation of the class names for the active arguments in the method signature.}\section{S3 Methods}{The functions for which S4 methods will be written often include somefor which S3 methods exist, corresponding to S3 classes for the firstformal argument of an S3 generic function or of a primitive function, or for either of thearguments in a call to one of the primitive binary operators.In the case of true functions, S3 methods will be dispatchedby the original version of the function. The usualway this happens is by the function becoming the defaultmethod for the S4 generic, implicitly by a call to\code{\link{setMethod}} or explicitly by the call\code{setGeneric("f")}where the original \code{f()} contained the call\code{UseMethod("f")}.The S4 method selection code matches the classes of the arguments asdescribed in the previous section.Matching will be applied for the class of S3 objects as well as S4objects, but only the first string in an S3 class attribute is used.If no non-default S4 methodmatches the call, the default S4 method can then operate as an S3generic to select S3 methods for \code{f()}.Primitive functions and operators dispatch both S4 and S3 methods fromthe internal C code.The method selection mechanism works essentially the same way, withtwo exceptions.There is no explicit generic function, either S3 or S4, meaning thatthe selection of an S3 method if no S4 method is found is built in andnot a result of an explicit default method.Also, the internal code does not look for S4 methods unless the firstargument or one of the arguments to an operator is an S4 object.S4 methods can be defined for an S3 generic function and an S3 class.But if the function is a primitive, such methods will not be selectedif the object in question is not an S4 object.In the examples below, for instance, an S4 method for signature\code{"data.frame"} for function \code{f3()} would be called for theS3 object \code{df1}.A similar S4 method for primitive function\code{`[`} would be ignored for that object, but would be called forthe S4 object \code{mydf1} that inherits from \code{"data.frame"}.It's an unfortunate inconsistency, but enforced by the passion forefficiency in dispatching methods for primitives.The common case is that objects from S4 classes will use S4 methods,except when the function's default definition is wanted. For example,if an S4 class extends one of the basic object types the base code forthat type may do what we want.Objects not from an S4 class will continue to follow S3 method selection.The rest of this section describes S3 method selection in two special cases.In one case, the S4 class contains an S3 class (and has ensured thatobjects have all the structure needed for the S3 class).In the second case, S3 methods have been written for an S4 class;that is, a function \emph{f.class}, where \emph{f} is an S3 genericfunction and \emph{class} is the name of an S4 class, other than aregistered S3 class.The first case is now supported and recommended, the second case isdiscouraged, but occasionally needed (see section 4 of the paper inthe references).The following rules define selection of an S3 method for an S4object. S4 objects are defined internally by a bit in the Cstructure. In practice, any object generated from an S4 class will bean S4 object, as well as the result of most computationson such objects. Older computations defined for non-S4 classes orobject types may or may not return S4 objects when the arguments aresuch objects.An S3 method will be selected applying the following criteria in order:\enumerate{\itemthe \emph{class} for the method matches the name of the S4 classexactly;\itemthe object has a slot \code{".S3Class"} and \emph{class} is selectedby S3 inheritance, treating that slot as the S3 class of the object;}The second criterion will apply if either the S4 class contains an S3class or the argument \code{S3methods=TRUE} was given to\code{\link{setClass}} for the class of the object or for one of itssuperclasses.If an S4 class extends an S3 class, and if no S4 methods take precedence, we expect that thecorrect S3 method for the inherited S3 class will be chosen.This will happen, so long as the S3 class has been registered by acall to \code{\link{setOldClass}}.If so, the object from the S4 class will inherit a special slotthat will be used as the class for S3 dispatch. Effectively, thisslot is a proxy for the class attribute expected by S3 dispatch. Itcan even vary in its inheritance between objects, as happens with someS3 classes, such as \code{\link{POSIXt}}, if the replacement versionof \code{\link{S3Class}} is used to set it.If the class so selected is one of the basic S3 classes,the object is converted to an S3 objectwith this vector as its class attribute.A second nonstandard situation arises when an S3 method has beenexplicitly written for an S4 class.Versions of \R up to and including 2.9.0 did not recognize S4inheritance in dispatching S3 methods, so that subclasses of the S4 classwould not then inherit the S3 method. The version of \R{} accompanying this documentationfixes this problem, to the extent practical, as follows.S3 method selection willresemble S4 selection for the same class \emph{if} the call to\code{\link{setClass}} has included the argument \code{S3methods =TRUE}. If not, the current behavior (\R 2.9.1) is to select S3 methodsdefined for this class, but not for its subclasses (largely for back compatibility; in future versions of \R, S3 methods may beignored for S4 classes unless \code{S3methods} is set.)The implementation uses the same special slot as above for inheritingfrom an S3 class. Subclasses of a class set this way will inherit thesame special slot and the same S3 method selection.It's even possible to set the slot in individual objects, as above,but the possibilities for confusion are serious.Looking in the other direction, it remains true that S4 selection has noknowledge of S3 methods.This can cause problems when a class that expects to inheritthe S3 method, \code{"classA"} in the example below, also inherits from another S4class. If that class inherits an S4 method for a function, no matter howindirectly, that S4 method will be selected for an object from\code{"classA"}, even though there is a directly defined S3 method.The S3 method can only be accessed through the default S4 method.These problems are relatively unlikely to occur, but anyone defining aclass that extends both S3 and S4 classes needs to be careful.}\section{Method Selection and Dispatch: Details}{When a call to a generic function is evaluated, a method is selected correspondingto the classes of the actual arguments in the signature.First, the cached methods table is searched for an exact match;that is, a method stored under the signature defined bythe string value of \code{class(x)} for each non-missingargument, and \code{"missing"} for each missing argument.If no method is found directly for the actual arguments in a call to ageneric function, an attempt is made to match the available methods tothe arguments by using the superclass information about the actual classes.Each class definition may include a list of one or more\emph{superclasses} of the new class.The simplest and most common specification is by the \code{contains=} argument inthe call to \code{\link{setClass}}.Each class named in this argument is a superclass of the new class.The S language has two additional mechanisms for definingsuperclasses.A call to\code{\link{setIs}} can create an inheritance relationship that is not the simple one ofcontaining the superclass representation in the new class.In this case, explicit methods are defined to relate the subclass andthe superclass.Also, a call to \code{\link{setClassUnion}} creates a union class thatis asuperclass of each of the members of the union.All three mechanisms are treated equivalently for purposes ofmethod selection: they define the \emph{direct} superclasses of aparticular class.For more details on the mechanisms, see \code{\link{Classes}}.The direct superclasses themselves mayhave superclasses, defined by any of the same mechanisms, andsimilarly for further generations. Putting all this information together producesthe full list of superclasses for this class.The superclass list is included in the definition of the class that iscached during the \R session.Each element of the list describes the nature of the relationship (see\code{\linkS4class{SClassExtension}} for details).Included in the element is a \code{distance} slot giving a numericdistance between the two classes.The distance is the path length for the relationship:\code{1} for direct superclasses (regardless of which mechanismdefined them), then \code{2} for the direct superclasses of thoseclasses, and so on.In addition, any class implicitly has class \code{"ANY"} as a superclass. Thedistance to \code{"ANY"} is treated as larger than the distance to anyactual class.The special class \code{"missing"} corresponding to missing argumentshas only \code{"ANY"} as a superclass, while \code{"ANY"} has nosuperclasses.When a class definition is created or modified, the superclassesare ordered, first by a stable sort of the all superclasses bydistance.If the set of superclasses has duplicates (that is, if some class isinherited through more than one relationship), these are removed, ifpossible, so that the list of superclasses is consistent with thesuperclasses of all direct superclasses.See the reference on inheritance for details.The information about superclasses is summarized when a classdefinition is printed.When a method is to be selected by inheritance, a search is made inthe table for all methods directly corresponding to a combination ofeither the direct class or one of its superclasses, for each argumentin the active signature.For an example, suppose there is only one argument in the signature and that the class ofthe corresponding object was \code{"dgeMatrix"} (from the recommended package\code{Matrix}).This class has two direct superclasses and through these 4 additional superclasses.Method selection finds all the methods in the table of directlyspecified methods labeled by one of these classes, or by\code{"ANY"}.When there are multiple arguments in the signature, each argument willgenerate a similar list of inherited classes.The possible matches are now all the combinations of classes from eachargument (think of the function \code{outer} generating an array ofall possible combinations).The search now finds all the methods matching any of this combinationof classes.For each argument, the position in the list of superclasses of thatargument's class defines which method or methods (if the same classappears more than once) match best.When there is only one argument, the best match is unambiguous.With more than one argument, there may be zero or one match that isamong the best matches for \emph{all} arguments.If there is no best match, the selection is ambiguous and a message isprinted noting which method was selected (the first methodlexicographicaly in the ordering) and what other methods could havebeen selected.Since the ambiguity is usually nothing the end user could control,this is not a warning.Package authors should examine their package for possible ambiguousinheritance by calling \code{\link{testInheritedMethods}}.When the inherited method has been selected, the selection is cachedin the generic function so that future calls with the same class willnot require repeating the search. Cached inherited selections arenot themselves used in future inheritance searches, since that could resultin invalid selections.If you want inheritance computations to be done again (for example,because a newly loaded package has a more direct method than onethat has already been used in this session), call\code{\link{resetGeneric}}. Because classes and methods involvingthem tend to come from the same package, the current implementationdoes not reset all generics every time a new package is loaded.Besides being initiated through calls to the generic function, methodselection can be done explicitly by calling the function\code{\link{selectMethod}}.Once a method has been selected, the evaluator creates a new contextin which a call to the method is evaluated.The context is initialized with the arguments from the call to thegeneric function.These arguments are not rematched. All the arguments in the signatureof the generic will have been evaluated (including any that arecurrently inactive); arguments that are not in the signature will obeythe usual lazy evaluation rules of the language.If an argument was missing in the call, its default expression if anywill \emph{not} have been evaluated, since method dispatch always usesclass \code{missing} for such arguments.A call to a generic function therefore has two contexts: one for thefunction and a second for the method.The argument objects will be copied to the second context, but not anylocal objects created in a nonstandard generic function.The other important distinction is that the parent(\dQuote{enclosing}) environment of the second context is the environmentof the method as a function, so that all \R programming techniquesusing such environments apply to method definitions as ordinary functions.For further discussion of method selection and dispatch, see thefirst reference.}\section{Generic Functions}{In principle, a generic function could be any function that evaluatesa call to \code{standardGeneric()}, the internal function that selectsa method and evaluates a call to the selected method. In practice,generic functions are special objects that in addition to being from asubclass of class \code{"function"} also extend the class\code{\linkS4class{genericFunction}}. Such objects have slots to defineinformation needed to deal with their methods. They also havespecialized environments, containing the tables used in methodselection.The slots \code{"generic"} and \code{"package"} in the object are thecharacter string names of the generic function itself and of thepackage from which the function is defined.As with classes, generic functions are uniquely defined in \R by thecombination of the two names.There can be generic functions of the same name associated withdifferent packages (although inevitably keeping such functions cleanlydistinguished is not always easy).On the other hand, \R will enforce that only one definition of ageneric function can be associated with a particular combination offunction and package name, in the current session or other activeversion of \R.Tables of methods for a particular generic function, in this sense,will often be spread over several other packages.The total set of methods for a given generic function may changeduring a session, as additional packages are loaded.Each table must be consistent in the signature assumed for the genericfunction.\R distinguishes \emph{standard} and \emph{nonstandard} genericfunctions, with the former having a function body that does nothingbut dispatch a method.For the most part, the distinction is just one of simplicity: knowingthat a generic function only dispatches a method call allows someefficiencies and also removes some uncertainties.In most cases, the generic function is the visible functioncorresponding to that name, in the corresponding package.There are two exceptions, \emph{implicit} genericfunctions and the special computations required to deal with \R's\emph{primitive} functions.Packages can contain a table of implicit generic versions of functionsin the package, if the package wishes to leave a function non-genericbut to constrain what the function would be like if it were generic.Such implicit generic functions are created during the installation ofthe package, essentially by defining the generic function andpossibly methods for it, and then reverting the function to itsnon-generic form. (See \link{implicitGeneric} for how this is done.)The mechanism is mainly used for functions in the older packages in\R, which may prefer to ignore S4 methods.Even in this case, the actual mechanism is only needed if somethingspecial has to be specified.All functions have a corresponding implicit generic version definedautomatically (an implicit, implicit generic function one might say).This function is a standard generic with the same arguments as thenon-generic function, with the non-generic version as the default (and only)method, and with the generic signature being all the formal argumentsexcept \dots.The implicit generic mechanism is needed only to override some aspectof the default definition.One reason to do so would be to remove some arguments from thesignature.Arguments that may need to be interpreted literally, or for which thelazy evaluation mechanism of the language is needed, must \emph{not}be included in the signature of the generic function, since allarguments in the signature will be evaluated in order to select amethod.For example, the argument \code{expr} to the function\code{\link{with}} is treated literally and must therefore be excludedfrom the signature.One would also need to define an implicit generic if the existingnon-generic function were not suitable as the default method.Perhaps the function only applies to some classes of objects, and thepackage designer prefers to have no general default method.In the other direction, the package designer might have some ideasabout suitable methods for some classes, if the function were generic.With reasonably modern packages, the simple approach in all thesecases is just to define the function as a generic.The implicit generic mechanism is mainly attractive for older packagesthat do not want to require the methods package to be available.Generic functions will also be defined but not obviously visible forfunctions implemented as \emph{primitive} functions in the basepackage.Primitive functions look like ordinary functions when printed but arein fact not function objects but objects of two types interpreted bythe \R evaluator to call underlying C code directly.Since their entire justification is efficiency, \R refuses to hideprimitives behind a generic function object.Methods may be defined for most primitives, and corresponding metadataobjects will be created to store them.Calls to the primitive still go directly to the C code, which willsometimes check for applicable methods.The definition of \dQuote{sometimes} is that methods must have beendetected for the function in some package loaded in the session and\code{isS4(x)} is \code{TRUE} for the first argument (or for thesecond argument, in the case of binary operators).You can test whether methods have been detected by calling\code{\link{isGeneric}} for the relevant function and you can examinethe generic function by calling \code{\link{getGeneric}}, whether ornot methods have been detected.For more on generic functions, see the first reference and also section 2 of \emph{R Internals}.}\section{Method Definitions}{All method definitions are stored as objects from the\code{\linkS4class{MethodDefinition}} class.Like the class of generic functions, this class extends ordinary \Rfunctions with some additional slots: \code{"generic"}, containing thename and package of the generic function, and two signature slots,\code{"defined"} and \code{"target"}, the first being the signature supplied whenthe method was defined by a call to \code{\link{setMethod}}.The \code{"target"} slot starts off equal to the \code{"defined"}slot. When an inherited method is cached after being selected, asdescribed above, a copy is made with the appropriate \code{"target"} signature.Output from \code{\link{showMethods}}, for example, includes bothsignatures.Method definitions are required to have the same formal arguments asthe generic function, since the method dispatch mechanism does notrematch arguments, for reasons of both efficiency and consistency.}\examples{## The rules for inheriting S3 methods.f3 <- function(x)UseMethod("f3") # an S3 generic to illustrate inheritance## A class that extends a registered S3 class inherits that class' S3## methods. The S3 methods will be passed an object with the S3 classsetClass("myFrame", contains = "data.frame",representation(date = "POSIXt", type = "character"))df1 <- data.frame(x = 1:10, y = rnorm(10), z = sample(letters,10))mydf1 <- new("myFrame", df1, date = Sys.time())## "myFrame" objects inherit "data.frame" S3 methods; e.g., for `[`mydf1[1:2, ] # a data frame object (with extra attributes "date" and "type")\dontshow{m1 <- mydf1[1:2,]attr(m1, "date") <- attr(m1, "type") <- NULLstopifnot(identical(m1, df1[1:2,]))}## Extending an S3 class with inconsistent (instance-based) inheritancesetClass("myDateTime", contains = "POSIXt")now <- Sys.time() # class(now) is c("POSIXt", "POSIXct")nowLt <- as.POSIXlt(now)# class(nowLt) is c("POSIXt", "POSIXlt")mCt <- new("myDateTime", now)mLt <- new("myDateTime", nowLt)## S3 methods will be selected using instance-based informationf3.POSIXct <- function(x) "The POSIXct result"f3.POSIXlt <- function(x) "The POSIXlt result"stopifnot(identical(f3(mCt), f3.POSIXct(mCt)))stopifnot(identical(f3(mLt), f3.POSIXlt(mLt)))## An S4 class that does not contain a registered S3 class or object type## selects S3 methods according to its S4 "inheritance"## but only if the class definition requests this via S3methods=TRUE## ( from version 2.9.1 on)setClass("classA", contains = "numeric",representation(realData = "numeric"), S3methods = TRUE)Math.classA <- function(x) {(getFunction(.Generic))(x@realData)}x <- new("classA", log(1:10), realData = 1:10)stopifnot(identical(abs(x), 1:10))setClass("classB", contains = "classA")y <- new("classB", x)stopifnot(identical(abs(y), 1:10)) # (version 2.9.0 or earlier fails here)## Note: with a class that tries to combine both S3 and S4 superclasses.## The S3 inheritance is used and the S3 method for## the S4 superclass will not be selected.setClass("classC", representation(x = "numeric"))# an S3 method for "[" (not a good idea, but it would work)`[.classc` <- function(x, ..., drop = TRUE) {x@x[...]}setClass("classD", contains = c("classC", "data.frame"))## by the rule mentioned in the S3 method section, the## S3 methods are selected from the S3 class defined; that is, "data.frame"## If the user expected to inherit `[.classC`, no luck.xd <- new("classD", df1, x = 1:50)## Note the error from `[.data.frame`try(xd[1:25])\dontshow{removeClass("classA"); removeClass("classB"); rm(x,y)removeClass("myDateTime")}}\references{Chambers, John M. (2008)\emph{Software for Data Analysis: Programming with R}Springer. (For the R version: see section 10.6 for methodselection and section 10.5 for generic functions).Chambers, John M.(2009)\emph{Developments in Class Inheritance and Method Selection}\url{http://stat.stanford.edu/~jmc4/classInheritance.pdf}.Chambers, John M. (1998)\emph{Programming with Data}Springer (For the original S4 version.)}\seealso{For more specific information, see\code{\link{setGeneric}}, \code{\link{setMethod}}, and\code{\link{setClass}}.For the use of \dots in methods, see \link{dotsMethods}.}\keyword{programming}\keyword{classes}\keyword{methods}