找回密码
 注册
查看: 1898|回复: 0

R语言:Methods()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 18:34:10 | 显示全部楼层 |阅读模式
Methods(methods)
Methods()所属R语言包:methods

                                        General Information on Methods
                                         在方法上的一般信息

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

This documentation section covers some general topics on how methods work and how the methods package interacts with the rest of R.  The information is usually not needed to get started with methods and classes, but may be helpful for moderately ambitious projects, or when something doesn't work as expected.
本文档的部分覆盖方法的工作原理以及如何methods包交互信息通常并不需要得到启动的方法和类,其余的河一些一般性的主题,但可能有助于中等雄心勃勃的项目,当事情并不如预期运作。

The section “How Methods Work” describes the underlying mechanism; “S3 Methods and Generic Functions” gives the rules applied when S4 classes and methods interact with older S3 methods; “Method Selection and Dispatch” provides more details on how class definitions determine which methods are used; “Generic Functions” discusses generic functions as objects. For additional information specifically about class definitions, see Classes.
节“方法的工作原理”,描述了基本的机制,“S3的方法和通用功能”给出的规则适用于中S4中的类和方法与旧的S3的方法进行交互时,“方法选择和调度”类定义如何提供更多的细节确定哪些方法使用“通用功能”的讨论为对象的通用功能。 ,特别是关于类定义的其他信息,请参阅Classes。


方法的工作原理----------How Methods Work----------

A generic function  has associated with it a collection of other functions (the methods), all of which have the same formal arguments as the generic.  See the “Generic Functions” section below for more on generic functions themselves.
一个泛型函数与它的其他功能(方法),都具有相同的形式参数作为通用的集合。查看更多“通用功能”一节的通用函数本身下面。

Each R package will include  methods metadata objects corresponding to each generic function  for which methods have been defined in that package. When the package is loaded into an R session, the methods for each generic function are cached, that is, stored in the environment of the generic function along with the methods from previously loaded packages.  This merged table of methods is used to dispatch or select methods from the generic, using class inheritance and possibly group generic functions (see GroupGenericFunctions) to find an applicable method. See the “Method Selection and Dispatch” section below. The caching computations ensure that only one version of each generic function is visible globally; although different attached packages may contain a copy of the generic function, these behave identically with respect to method selection. In contrast, it is possible for the same function name to refer to more than one generic function, when these have different package slots.  In the latter case, R considers the functions unrelated:  A generic function is defined by the combination of name and package.  See the “Generic Functions” section below.
每个R包将包括方法对应到每一个泛型函数,该方法已在该包中定义的元数据对象。当包被加载到一个R会话,每个泛型函数的方法是缓存,即存储环境中的通用功能从以前加载的包的方法。用这种方法合并表派遣或选择从通用的方法,使用类的继承和可能组通用功能(见GroupGenericFunctions)来找到一个适用的方法。看到“方法的选择和调度”一节。缓存的计算确保每个泛型函数只有一个版本是全局可见的,虽然不同的附加包可能包含一个泛型函数的副本,这些行为与方法的选择方面完全相同。相反,它可能是相同的函数名来指超过一个通用的功能,当这些具有不同package插槽。在后一种情况下,研究认为,功能无关:定义一个泛型函数的名称和包组合。看到下面的“通用功能”一节。

The methods for a generic are stored according to the corresponding signature in the call to setMethod that defined  the method.  The signature associates one class name with each of a subset of the formal arguments to the generic function.  Which formal arguments are available, and the order in which they appear, are determined by the "signature" slot of the generic function itself.  By default, the signature of the generic consists of all the formal arguments except ..., in the order they appear in the function definition.
一个通用的方法是根据相应的存储signature在调用setMethod定义的方法。签名联营公司与每个泛型函数的形式参数的一个子集类的名称。正式参数是可用的,它们出现的顺序,确定"signature"泛型函数本身的插槽。默认情况下,通用的签名,包括所有形式参数除...的顺序,它们出现在函数定义。

Trailing arguments in the signature of the generic will be inactive  if no method has yet been specified that included those arguments in its signature. Inactive arguments are not needed or used in labeling the cached methods.  (The distinction does not change which methods are dispatched, but ignoring inactive arguments improves the efficiency of dispatch.)
尾随通用的签名中的参数将是无效的,如果没有方法尚未指定这些参数包括在其签名。无效的参数并不需要,或在标签缓存的方法使用。 (区别并不会改变派遣的方法,但忽略无效的参数,提高调度的效率。)

All arguments in the signature of the generic function will be evaluated when the function is called, rather than using the traditional lazy evaluation rules of S.  Therefore, it's important to exclude from the signature any arguments that need to be dealt with symbolically (such as the first argument to function substitute).  Note that only actual arguments are evaluated, not default expressions. A missing argument enters into the method selection as class "missing".
在签名的通用功能的所有参数的函数被调用时,将被评估,而不是使用与传统的懒惰的评价规则,因此,重要的是从排除任何参数,需要象征性地处理(如签名第一个参数的功能substitute)。请注意,只有实际参数进行评估,而不是默认的表达式。缺少论据进入类"missing"方法的选择。

The cached methods are stored in an environment object.  The names used for assignment are a concatenation of the class names for the active arguments in the method signature.
环境对象存储在缓存的方法。分配使用的名称是串联活动参数在方法签名中的类名。


S3的通用功能的方法----------Methods for S3 Generic Functions----------

S4 methods may be wanted for functions that also have S3 methods, corresponding to classes for the first formal argument of an S3 generic function–either a regular R function in which there is a call to the S3 dispatch function, UseMethod, or one of a fixed set of primitive functions, which are not true functions but go directly to C code. In either case S3 method dispatch looks at the class of the first argument or the class of either argument in a call to one of the primitive binary operators. S3 methods are ordinary functions with the same arguments as the generic function (for primitives the formal arguments are not actually part of the object, but are simulated when the object is printed or viewed by args()). The “signature” of an S3 method is identified  by the name to which the method is assigned, composed of the name of the generic function, followed by ".", followed by the name of the class. For details, see S3Methods.
中S4中的方法可能被通缉的功能,也有S3的方法,一个S3通用功能,无论是一个普通的R函数,其中有S3调度功能的调用,UseMethod,首次正式参数的类对应或一个固定的原始功能,这是不正确的功能,但直接进入C代码集之一。 S3方法调度,在这两种情况下的第一个参数或调用原始的二进制运营商之一,在任一参数类的类。 S3的方法与普通的通用功能相同的参数功能(原语为正式的参数是不实际的对象的一部分,但模拟对象时,印刷或args()看,)。 “签名”的S3方法确定分配方法的名称".",其次类的名称,名称的通用功能组成。有关详细信息,看到S3Methods“。

To implement a method for one of these functions corresponding to S4 classes, there are two possibilities: either an S4 method or an S3 method with the S4 class name. The S3 method is only possible if the intended signature has the first argument and nothing else. In this case, the recommended approach is to define the S3 method and also supply the identical function as the definition of the S4 method. If the S3 generic function was f3(x, ...) and the S4 class for the new method was  "myClass":
为了实现这些功能对应到S4类的方法,有两种可能性:要么S4方法或S3方法与S4类的名称。 S3的方法是唯一可行的,如果打算签名的第一个参数,没有别的。在这种情况下,建议的做法是定义S3方法和S4的方法的定义,还提供相同的功能。如果是S3泛型函数f3(x, ...)和新方法S4类是"myClass":

f3.myClass <- function(x, ...) { ..... }
f3.myClass <- function(x, ...) { ..... }

setMethod("f3", "myClass", f3.myClass)
setMethod("f3", "myClass", f3.myClass)

The reasons for defining both S3 and S4 methods are as follows:
S3和S4方法定义的原因如下:

An S4 method alone will not be seen if the S3 generic function is called directly.  However, primitive functions and operators are exceptions:  The internal C code will look for S4 methods if and only if the object is an S4 object.  In the examples, the method for `[` for class "myFrame" will always be called for objects of this class.
单独一个中四的方法将不会看到,如果S3泛型函数直接调用。然而,原始的职能和运营商也有例外:内部的C代码将S4的方法,当且仅当对象是中四的对象。在例子中,[类"myFrame"将永远为这个类的对象调用的方法。

For the same reason, an S4 method defined for an S3 class will not be called from internal code for a non-S4 object. (See the example for function Math and class "data.frame" in the examples.)
出于同样的原因,中S4中S3类中定义的方法将不能被称为一个非S4对象从内部代码。 (参见功能Math“类的例子"data.frame"的例子。)

An S3 method alone will not be called if there is any eligible non-default S4 method. (See the example for function f3 and class "classA" in the examples.)
一个S3方法不会被调用,如果有任何合资格的非默认的S4方法。 (参见功能f3“类的例子"classA"的例子。)

Details of the selection computations are given below.
选择计算的详细情况如下。

When an S4 method is defined for an existing function that is not an S4 generic function (whether or not the existing function is an S3 generic), an S4 generic function will be created corresponding to the existing function and the package in which it is found (more precisely, according to the implicit generic function either specified or inferred from the ordinary function; see implicitGeneric). A message is printed after the initial call to setMethod; this is not an error, just a reminder that you have created the generic. Creating the generic explicitly by the call
当S4方法定义为现有的功能,是中S4中的通用功能(是否现有的功能是S3通用),中S4中的通用功能,将创建相应的现有功能,在它被发现的包(更确切地说,按照指定或从普通函数推断隐含的通用功能;看到implicitGeneric)。的消息后,最初调用印setMethod,这是不是一个错误,只是一个提醒,您已经创建了通用。通用明确创建呼叫

setGeneric("f3")
setGeneric("f3")

avoids the message, but has the same effect. The existing function becomes the default method for the S4 generic function. Primitive functions work the same way, but the S4 generic function is not explicitly created (as discussed below).
避免了消息,但具有相同的效果。现有的功能,成为为S4泛型函数的默认方法。原始功能的工作方式相同,但S4的通用功能不明确创建(下面讨论)。

S4 and S3 method selection are designed to follow compatible rules of inheritance, as far as possible. S3 classes can be used for any S4 method selection, provided that the S3 classes have been registered by a call to setOldClass, with that call specifying the correct S3 inheritance pattern. S4 classes can be used for any S3 method selection; when an S4 object is detected, S3 method selection uses the contents of extends(class(x)) as the equivalent of the S3 inheritance (the inheritance is cached after the first call).
S4和S3的方式选择设计,尽可能遵循兼容的继承规则。 S3类可用于任何S4方法的选择,提供通过调用已注册的S3类setOldClass调用指定正确的S3继承模式。中S4中的类可用于任何S3方法的选择;检测S4对象时,S3方法选择使用的内容extends(class(x))作为相当于S3的继承(继承后第一次调用缓存)。

An existing S3 method may not behave as desired for an S4 subclass, in which case utilities such as asS3 and S3Part may be useful.  If the S3 method fails on the S4 object, asS3(x) may be passed instead; if the object returned by the S3 method needs to be incorporated in the S4 object, the replacement function for S3Part may be useful, as in the method for class "myFrame" in the examples.
根据需要,现有S3方法可能不会表现为S4的子类,如公用事业,其中asS3和S3Part可能是有用的。如果S3的S4对象的方法失败,asS3(x)可以通过,而不是由S3方法返回的对象,如果需要在S4对象纳入的更换S3Part“功能可能是有用的,在方法类"myFrame"的例子。

Here are details explaining the reasons for defining both S3 and S4 methods. Calls still accessing the S3 generic function directly will not see S4 methods, except in the case of primitive functions. This means that calls to the generic function from namespaces that import the S3 generic but not the S4 version will only see S3 methods. On the other hand, S3 methods will only be selected from the S4 generic function as part of its default ("ANY") method. If there are inherited S4 non-default methods, these will be chosen in preference to any S3 method.
这里是S3和S4方法定义的原因解释的细节。直接调用仍然访问S3泛型函数,将不会看到S4方法,在原始功能的情况下除外。这意味着从导入通用的,但不是S4版本的S3的命名空间的通用功能的调用,将只能看到S3的方法。另一方面,S3方法只会被选中作为其默认的一部分("ANY")方法从S4的通用功能。如果有继承中S4中的非默认的方法,这些将被优先选择的任何S3方法。

S3 generic functions implemented as primitive functions (including binary operators) are an exception to recognizing only S3 methods. These functions dispatch both S4 and S3 methods from the internal C code. There is no explicit generic function, either S3 or S4. The internal code looks for S4 methods if the first argument, or either of the arguments in the case of a binary operator, is an S4 object. If no S4 method is found, a search is made for an S3 method.
作为原始的功能(包括二元运算符)实现S3的通用功能都承认只有S3的方法的一个例外。这些功能派遣S4和S3的方法,从内部的C代码。有没有明确的通用功能,S3或S4。如果第一个参数,或一方在一个二元运算符的情况下的参数,是一个中四的对象为中S4中方法内部的代码。如果没有发现S4方法,搜索为S3的方法。

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 selected if the object in question is not an S4 object. In the examples below, for instance,  an S4 method for signature "data.frame" for function f3() would be called for the S3 object df1. A similar S4 method for primitive function `[` would be ignored for that object, but would be called for the S4 object mydf1 that inherits from "data.frame". Defining both an S3 and S4 method removes this inconsistency.
中S4中方法可以定义为S3泛型函数和一个S3类,但如果该函数是一个原始的,这种方法不会被选中,如果有问题的对象是不是S4对象。在下面的例子,例如,S4签名方法的"data.frame"为函数f3()将呼吁S3对象df1。类似的原始功能[S4方法将忽略该对象,但将被称为mydf1,"data.frame"继承的S4对象。定义一个S3和S4方法,消除这种不一致。


方法的选择和调度:详细----------Method Selection and Dispatch: Details----------

When a call to a generic function is evaluated, a method is selected corresponding to 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 by the string value of class(x) for each non-missing argument, and "missing" for each missing argument. If no method is found directly for the actual arguments in a call to a generic function, an attempt is made to match the available methods to the arguments by using the superclass information about the actual classes.
当评估一个泛型函数的调用,一种方法是选择相应的签名中的实际参数的类。首先,表缓存的方法是精确匹配搜索,也就是说,下class(x)每个非缺少的参数,和"missing"每个缺少的参数的字符串值定义的签名存储方法。如果没有一种方法是直接在调用泛型函数的实际参数,试图以匹配可用的方法,通过使用有关的实际类的超类信息的论点。

Each class definition may include a list of  one or more superclasses of the new class. The simplest and most common specification is by the contains= argument in the  call to setClass. Each class named in this argument is a superclass of the new class. Two additional mechanisms for defining superclasses exist. A call to setClassUnion creates a union class that is a superclass of each of the members of the union. A call to  setIs can create an inheritance relationship that is not the simple one of containing the superclass representation in the new class. Arguments coerce and replace supply methods to convert to the superclass and to replace the part corresponding to the superclass. (In addition, a test= argument allows conditional inheritance;  conditional inheritance is not recommended and is not used in method selection.) All three mechanisms are treated equivalently for purposes of method selection:  they define the direct superclasses of a particular class. For more details on the mechanisms, see Classes.
每个类定义可以包括一个或多个新的类的超类的列表。最简单和最常见的规格是的contains=调用setClass在参数。每个类名为这种说法是一个新的类的超类。定义父的两个额外的机制存在。一个setClassUnion调用创建一个工会是每个工会成员的超类。一个setIs调用可以创建一个继承的关系,是不含有超表示在新的类的简单。参数coerce和replace供应方式转换为超类,并更换部分相应的超。 (此外,test=参数允许有条件的继承;不建议有条件的继承和方法的选择不使用。)所有三种机制等价的治疗方法选择的目的:他们定义一个特定的类的直接超类。对于机制的更多细节,请参阅Classes。

The direct superclasses themselves may have superclasses, defined by any of the same mechanisms, and similarly through further generations.  Putting all this information together produces the full list of superclasses for this class. The superclass list is included in the definition of the class that is cached during the R session. Each element of the list describes the nature of the relationship (see SClassExtension for details). Included in the element is a distance slot containing the path length for the relationship: 1 for direct superclasses (regardless of which mechanism defined them), then 2 for the direct superclasses of those classes, and so on. In addition, any class implicitly has class "ANY" as a superclass.  The distance to "ANY" is treated as larger than the distance to any actual class. The special class "missing" corresponding to missing arguments has only "ANY" as a superclass, while "ANY" has no superclasses.
直接超类本身可能有父相同的机制,任何定义,同样通过进一步的后代。把所有这些信息产生了这个类的父类的完整列表。类名单中定义的类,它是在R会话缓存。列表中的每个元素描述了这种关系的性质(见SClassExtension细节)。中包含的元素是一个distance插槽包含关系的路径长度:1直接超类(无论哪个机制定义),然后2这些类的直接超类,等等。此外,任何类隐式类"ANY"作为一个超。的"ANY"的距离被视为比任何实际的类之间的距离大。类特殊"missing"缺少参数对应有唯一"ANY"作为一个超类,而"ANY"有没有父。

When a class definition is created or modified, the superclasses are ordered, first by a stable sort of the all superclasses by distance. If the set of superclasses has duplicates (that is, if some class is inherited through more than one relationship), these are removed, if possible, so that the list of superclasses is consistent with the superclasses of all direct superclasses. See the reference on inheritance for details.
当创建或修改一个类的定义,父是有序的,首先由距离所有超稳定的排序。父集,如果有重复的(也就是说,如果某个类是通过继承多个关系),这些被删除,如果可能的话,这样的父列表所有直接父类的父是一致的。继承详情,请参阅参考。

The information about superclasses is summarized when a class definition is printed.
印刷类定义时,父类有关的信息进行了总结。

When a method is to be selected by inheritance, a search is made in the table for all methods directly corresponding to a combination of either the direct class or one of its superclasses, for each argument in the active signature. For an example, suppose there is only one argument in the signature and that the class of the corresponding object was "dgeMatrix" (from the recommended package Matrix). This class has two direct superclasses and through these 4 additional superclasses. Method selection finds all the methods in the table of directly specified methods labeled by one of these classes, or by "ANY".
当一个方法是通过继承选择,搜索表中的所有直接对应于直接其超类或组合的方法,为每个参数在活动签名。举一个例子,假设只有一个参数在签名和相应的对象类"dgeMatrix"(从推荐的包Matrix)。这个类有两个直接超类,并通过这4个额外的父。方法的选择,发现在这些类的一个标记方法直接指定的表,或由"ANY"所有的方法。

When there are multiple arguments in the signature, each argument will generate a similar  list of inherited classes. The possible matches are now all the combinations of classes from each argument (think of the function outer generating an array of all possible combinations). The search now finds all the methods matching any of this combination of classes. For each argument, the position in the list of superclasses of that argument's class defines which method or methods (if the same class appears 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 is among the best matches for all arguments.
在签名当有多个参数,每个参数将产生一个继承类的类似名单。可能的匹配,现在从每个参数组合类(想到的功能outer生成所有可能的组合阵列)。现在的搜索找到的所有方法匹配任何这一类的组合。对于每一个论点,这一论点的类的超类名单中的位置定义的方法或方法(如果同一类出现不止一次)匹配最好的。当有是只有一个参数,是毫不含糊的最佳匹配。与多个参数,有可能是零个或一个匹配之间的所有参数的最佳匹配。

If there is no best match, the selection is ambiguous and a message is printed noting which method was selected (the first method lexicographically in the ordering) and what other methods could have been 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 ambiguous inheritance by calling testInheritedMethods.
如果没有最佳匹配,选择是明确的,并指出哪种方法被选中(字典排序的第一种方法),并已选择什么其他方法可以打印一条消息。歧义是由于通常没有最终用户可以控制,这是一个警告。包作者应检查其可能的模棱两可的继承包调用testInheritedMethods。

When the inherited method has been selected, the selection is cached in the generic function so that future calls with the same class will not require repeating the search.  Cached inherited selections are not themselves used in future inheritance searches, since that could result in invalid selections. If you want inheritance computations to be done again (for example, because a newly loaded package has a more direct method than one that has already been used in this session), call resetGeneric.  Because classes and methods involving them tend to come from the same package, the current implementation does not reset all generics every time a new package is loaded.
当已选定继承的方法,选择的是缓存中的通用功能,以便将来调用同一类将不需要重复搜索。缓存继承的选择不是自己在未来的继承搜索,因为这可能会导致无效的选择。如果你想继承再次进行计算(例如,由于新加载的包有多个,已在本次会议更直接的方法),调用resetGeneric。因为它们涉及的类和方法往往来自同一个包,目前的执引号况并不重置所有泛型每一个新的包加载时间。

Besides being initiated through calls to the generic function, method selection can be done explicitly by calling the function selectMethod.
除了开始通过调用泛型函数,方法的选择可以通过调用函数selectMethod做了明确。

Once a method has been selected, the evaluator creates a new context in which a call to the method is evaluated. The context is initialized with the arguments from the call to the generic function. These arguments are not rematched.  All the arguments in the signature of the generic will have been evaluated (including any that are currently inactive); arguments that are not in the signature will obey the usual lazy evaluation rules of the language. If an argument was missing in the call, its default expression if any will not have been evaluated, since method dispatch always uses class missing for such arguments.
评估方法已被选中,创建一个新的上下文中调用方法进行评估。从泛型函数调用的参数初始化上下文。这些参数是不是重新匹配。通用的签名中的所有参数将被评估(包括任何目前无效);参数是在签名会服从的语言一贯的懒惰的评价规则。如果在通话中缺少一个参数,其默认的表达,如果没有将不会被评估,因为调度方法总是使用类missing这种论调。

A call to a generic function therefore has two contexts:  one for the function and a second for the method. The argument objects will be copied to the second context, but not any local objects created in a nonstandard generic function. The other important distinction is that the parent  (&ldquo;enclosing&rdquo;) environment of the second context is the environment of the method as a function, so that all R programming techniques using such environments apply to method definitions as ordinary functions.
因此,一个泛型函数的调用有两个背景:一个函数和方法的第二。参数对象将被复制到第二个方面,但没有任何地方的对象创建在一个非标准的通用功能。其他重要的区别是,父(“封闭”)的第二个方面的环境是作为一个函数的方法的环境,使所有的R编程技术,使用这样的环境中,适用于普通函数的方法定义。

For further discussion of method selection and dispatch,  see the first reference.
为进一步讨论方法的选择和调度,看到的第一个参考。


通用功能----------Generic Functions----------

In principle, a generic function could be any function that evaluates a call to standardGeneric(), the internal function that selects a method and evaluates a call to  the selected method.  In practice, generic functions are special objects that in addition to being from a subclass of class "function" also extend the class genericFunction.  Such objects have slots to define information needed to deal with their methods.  They also have specialized environments, containing the tables used in method selection.
在原则上,一个通用的功能可能是standardGeneric(),选择的方法和评估选定的方法调用内部函数调用任何函数的计算结果。在实践中,泛型函数是特殊的对象,除了以从"function"类的子类是扩展类genericFunction。这样的对象必须插槽定义对付他们的方法所需的信息。他们也有专门的环境中,包含在方法的选择使用的表。

The slots "generic" and  "package" in the object are the character string names of the generic function itself and of the package from which the  function is defined. As with classes, generic functions are uniquely defined in R by the combination of the two names. There can be generic functions of the same name associated with different packages (although inevitably keeping such functions cleanly distinguished is not always easy). On the other hand, R will enforce that only one definition of a generic function can be associated with a particular combination of function and package name, in the current session or other active version of R.
插槽"generic"和"package"对象中的字符串泛型函数本身和从该函数定义的包名。至于类,泛型函数是唯一定义在R,由这两个名字的组合。可以有不同的包的名称相同的通用功能(尽管不可避免地保持干净区分等功能并不总是很容易)。另一方面,R将强制执行,只有一个泛型函数的定义可以与相关的功能和包名的特定组合,在本届会议或其他活动版本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 change during a session, as additional packages are loaded. Each table must be consistent in the signature assumed for the generic function.
表特定的通用功能的方法,在这个意义上说,往往会被遍布其他几个包。对于一个给定的通用功能的方法总集,可能会改变在会议期间,作为加载额外的软件包。每个表必须是在泛型函数假设的签名一致。

R distinguishes standard and nonstandard generic functions, with the former having a function body that does nothing but dispatch a method. For the most part, the distinction is just one of simplicity:  knowing that a generic function only dispatches a method call allows some efficiencies and also removes some uncertainties.
26&#345;区分标准和非标准的通用功能,与前者有什么也不做,但派遣一个方法函数体。在大多数情况下,区别只是一个简单:知道一个泛型函数只派遣一个方法调用允许一些效率,也消除一些不确定性。

In most cases, the generic function is the visible function corresponding to that name, in the corresponding package. There are two exceptions, implicit generic functions and the special computations required to deal with R's primitive functions. Packages can contain a table of implicit generic versions of functions in the package, if the package wishes to leave a function non-generic but to constrain what the function would be like if it were generic. Such implicit generic functions are created during the installation of the package, essentially by defining the generic function and possibly methods for it, and then reverting the function to its non-generic form. (See 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 something special has to be specified. All functions have a corresponding implicit generic version defined automatically (an implicit, implicit generic function one might say). This function is a standard generic with the same arguments as the non-generic function, with the non-generic version as the default (and only) method, and with the generic signature being all the formal arguments except ....
在大多数情况下,通用的功能是可见的功能,该名称对应,在相应的软件包。有两个例外,隐含的通用功能和处理R的原始功能所需的特殊计算。包可以包含一个包中的函数的隐式通用版本的表,如果包希望留下一个非泛型函数,但限制一样,如果它是通用的功能将是什么。这种隐含的通用功能包安装过程中创建的,基本上是由定义的通用功能,并可能为它的方法,然后恢复功能,其非泛型形式。 (见implicitGeneric如何做到这一点。)的机制,主要用于函数在R中的老包,这可能更愿意忽略S4方法。即使在这种情况下,实际的机制,只需要一些特别的东西,如果指定。所有功能都有相应的隐式自动定义(一个隐式的,隐含的通用功能,可以说)的通用版本。此功能是作为非通用功能相同的参数与非泛型版本作为默认方法(只),与通用的签名是所有的正式参数,除了标准的通用....

The implicit generic mechanism is needed only to override some aspect of the default definition. One reason to do so would be to remove some arguments from the signature. Arguments that may need to be interpreted literally, or for which the lazy evaluation mechanism of the language is needed, must not be included in the signature of the generic function, since all arguments in the signature will be evaluated in order to select a method. For example, the argument expr to the function with is treated literally and must therefore be excluded from the signature.
隐式的通用机制,只需要覆盖默认定义的某些方面。这样做的原因之一是从签名中删除一些参数。参数可能需要字面解释,或语言懒惰的评价机制是必要的,不能被包含在泛型函数的签名,因为签名中的所有参数将被评估,以选择一个方法。例如,参数函数exprwith被视为字面上,因此必须排除从签名。

One would also need to define an implicit generic if the existing non-generic function were not suitable as the default method. Perhaps the function only applies to some classes of objects, and the package designer prefers to have no general default method. In the other direction, the package designer might have some ideas about suitable methods for some classes, if the function were generic. With reasonably modern packages, the simple approach in all these cases is just to define the function as a generic. The implicit generic mechanism is mainly attractive for older packages that do not want to require the methods package to be available.
还需要定义一个隐含的通用,如果现有的非泛型功能,不适合作为默认的方法。也许功能只适用于某些对象类,包设计师喜欢有没有一般的默认方法。在其他方向,包设计师可能有合适的方法的一些想法,一些班级的功能,如果是通用。在所有这些情况下的简单的方法是合理的现代包装,只是作为一个通用的定义的功能。隐式的通用机制,主要是旧的包,不想要求的方法包可吸引力。

Generic functions will also be defined but not obviously visible for functions implemented as primitive functions in the base package. Primitive functions look like ordinary functions when printed but are in fact not function objects but objects of two types interpreted by the R evaluator to call underlying C code directly. Since their entire justification is efficiency, R refuses to hide primitives behind a generic function object. Methods may be defined for most primitives, and corresponding metadata objects will be created to store them. Calls to the primitive still go directly to the C code, which will sometimes check for applicable methods. The definition of &ldquo;sometimes&rdquo; is that methods must have been detected for the function in some package loaded in the session and isS4(x) is TRUE for  the first argument (or for the second argument, in the case of binary operators). You can test whether methods have been detected by calling isGeneric for the relevant function and you can examine the generic function by calling getGeneric, whether or not methods have been detected. For more on generic functions, see the first reference and also section 2 of R Internals.
通用功能也将被定义,但在基础包的原始功能实现的功能没有明显可见。原始函数打印时看起来像普通的功能,但实际上不是函数对象,但解释的R计算器,直接调用底层的C代码的两种类型的对象。由于他们的整个理由是效率,研究拒绝一个泛型函数对象的背后隐藏的原语。方法可以被定义为最基元,将建立相应的元数据对象来存储它们。原始的呼叫仍然直接进入到C代码,这有时会检查适用的方法。 “有时”的定义是,方法已用于功能检测了会议,并预装了一些包isS4(x)是TRUE的第一个参数(或第二个参数的情况下,二元运算符)。您可以测试是否已通过调用isGeneric有关的功能,你可以检查通过调用getGeneric,方法是否已被侦破的通用功能检测方法。通用功能的更多信息,请参阅第一参考和R的内部第2。


方法定义----------Method Definitions----------

All method definitions are stored as objects from the MethodDefinition class. Like the class of generic functions, this class extends ordinary R functions with some additional slots: "generic", containing the name and package of the generic function, and two signature slots, "defined" and "target", the first being the signature supplied when the method was defined by a call to setMethod. The  "target" slot starts off equal to the "defined" slot.  When an inherited method is cached after being selected, as described above, a copy is made with the  appropriate "target"  signature. Output from showMethods, for example, includes both signatures.
所有的方法定义存储作为MethodDefinition类的对象。像通用功能的类,这个类扩展了一些额外的插槽普通R函数:"generic",包含泛型函数的名称和包装,和两个签名插槽,"defined"和"target" ,第一时提供的方法调用setMethod由定义的签名。 "target"槽开始起飞等于"defined"插槽。继承的方法是当缓存被选中后,如上所述,副本作出适当"target"签名。从showMethods输出,例如,包括签名。

Method definitions are required to have the same formal arguments as the generic function, since the method dispatch mechanism does not rematch arguments, for reasons of both efficiency and consistency.
方法定义必须有相同的形式参数作为通用功能,因为该方法调度机制不复赛参数,效率和一致性的原因。


参考文献----------References----------

Software for Data Analysis: Programming with R Springer.  (For the R version: see section 10.6 for method selection and section 10.5 for generic functions).
Developments in Class Inheritance and Method Selection http://stat.stanford.edu/~jmc4/classInheritance.pdf.
Programming with Data Springer (For the original S4 version.)

参见----------See Also----------

For more specific information, see setGeneric, setMethod, and setClass.
为更具体的信息,请参阅setGeneric,setMethod,setClass。

For the use of ... in methods, see  dotsMethods.
对于使用...方法见dotsMethods。


举例----------Examples----------


## A class that extends a registered S3 class inherits that class' S3[#A类,扩展了注册的S3类继承这个类中三]
## methods.[#方法。]

setClass("myFrame", contains = "data.frame",
    representation(timestamps = "POSIXt"))

df1 <- data.frame(x = 1:10, y = rnorm(10), z = sample(letters,10))

mydf1 <- new("myFrame", df1, timestamps = Sys.time())

## "myFrame" objects inherit "data.frame" S3 methods; e.g., for `[`[#“MyFrame的”对象继承“数据框”S3的方法,例如,[]

mydf1[1:2, ] # a data frame object (with extra attributes)[一个数据框对象(额外的属性)]

## a method explicitly for "myFrame" class[#方法MyFrame的“类明确]


setMethod("[",
    signature(x = "myFrame"),
    function (x, i, j, ..., drop = TRUE)
    {
        S3Part(x) <- callNextMethod()
        x@timestamps <- c(Sys.time(), as.POSIXct(x@timestamps))
        x
    }
)

mydf1[1:2, ]


setClass("myDateTime", contains = "POSIXt")

now &lt;- Sys.time() # class(now) is c("POSIXct", "POSIXt")[类(现在)是c(“POSIXct”,“POSIXt”)]
nowLt &lt;- as.POSIXlt(now)# class(nowLt) is c("POSIXlt", "POSIXt")[类(nowLt)是c(“POSIXlt”,“POSIXt”)]

mCt <- new("myDateTime", now)
mLt <- new("myDateTime", nowLt)

## S3 methods for an S4 object will be selected using S4 inheritance[#S3为S4对象的方法将使用中S4中继承]
## Objects mCt and mLt have different S3Class() values, but this is[#对象MCT和MLT有不同的价值观S3Class(),但这是]
## not used.[#不使用。]
f3 &lt;- function(x)UseMethod("f3") # an S3 generic to illustrate inheritance[一个S3通用说明继承]

f3.POSIXct <- function(x) "The POSIXct result"
f3.POSIXlt <- function(x) "The POSIXlt result"
f3.POSIXt <- function(x) "The POSIXt result"

stopifnot(identical(f3(mCt), f3.POSIXt(mCt)))
stopifnot(identical(f3(mLt), f3.POSIXt(mLt)))



## An S4 object selects S3 methods according to its S4 "inheritance"[#S4对象选择S3的方法,根据其S4“继承”]


setClass("classA", contains = "numeric",
   representation(realData = "numeric"))

Math.classA <- function(x) {(getFunction(.Generic))(x@realData)}
setMethod("Math", "classA", Math.classA)


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)[(2.9.0或更早版本失败)]

## an S3 generic: just for demonstration purposes[#S3通用:只是用于演示目的]
f3 <- function(x, ...) UseMethod("f3")

f3.default <- function(x, ...) "Default f3"

## S3 method (only) for classA[#S3的ClassA的方法(只)]
f3.classA <- function(x, ...) "Class classA for f3"

## S3 and S4 method for numeric[#S3和S4的数值计算方法]
f3.numeric <- function(x, ...) "Class numeric for f3"
setMethod("f3", "numeric", f3.numeric)

## The S3 method for classA and the closest inherited S3 method for classB[#S3的ClassA的方法和最亲密的继承S3的ClassB的方法]
## are not found.[#都没有发现。]

f3(x); f3(y) # both choose "numeric" method[同时选择“数字”的方法]

## to obtain the natural inheritance, set identical S3 and S4 methods[#获得自然遗产,设置相同的S3和S4的方法]
setMethod("f3", "classA", f3.classA)

f3(x); f3(y) # now both choose "classA" method[现在无论是选择“超力旺”的方法]

## Need to define an S3 as well as S4 method to use on an S3 object[#需要定义一个S3以及S4方法的使用S3对象]
## or if called from a package without the S4 generic[#或者叫包没有S4通用]

MathFun &lt;- function(x) { # a smarter "data.frame" method for Math group[一个聪明的“数据框”的方法,数学组]
  for (i in seq(length = ncol(x))[sapply(x, is.numeric)])
    x[, i] <- (getFunction(.Generic))(x[, i])
  x
}
setMethod("Math", "data.frame", MathFun)

## S4 method works for an S4 class containing data.frame,[#S4方法为S4类包含数据框,]
## but not for data.frame objects (not S4 objects)[#而不是为数据框对象(没有S4对象)]

try(logIris &lt;- log(iris)) #gets an error from the old method[从旧方法错误]

## Define an S3 method with the same computation[#定义具有相同的计算S3方法]

Math.data.frame <- MathFun

logIris <- log(iris)







转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-23 15:05 , Processed in 0.045596 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表