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

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

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

                                        The Use of ... in Method Signatures
                                         使用......在方法签名

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

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

The “...” argument in R functions is treated specially, in that it matches zero, one or more actual arguments (and so, objects).  A mechanism has been added to R to allow “...” as the signature of a generic function.  Methods defined for such functions will be selected and called when all  the arguments matching “...” are from the specified class or from some subclass of that class.
“......”R函数的参数是特殊对待,它匹配一个或多个零,实际参数(所以,对象)。一个机制已被添加到R允许“......”作为一个泛型函数的签名。定义等功能的方法将被选中时调用所有参数匹配“......”从指定的类或从一些该类的子类。


用“......”签名----------Using "..." in a Signature----------

Beginning with version 2.8.0 of R, S4 methods can be dispatched (selected and called) corresponding to the special argument “...”. Currently, “...” cannot be mixed with other formal arguments: either the signature of the generic function is “...” only, or it does not contain “...”.  (This restriction may be lifted in a future version.)
S4方法与R的2.8.0版本开始,可以派出(选择和调用)相应的特殊参数“...”。目前,“......”能不能混有其他形式参数:无论是通用函数的签名是“......”只是,它不包含“...”。 (此限制,可以解除在未来的版本。)

Given a suitable generic function, methods are specified in the usual way by a call to setMethod.  The method definition must be written expecting all the arguments corresponding to “...” to be from the class specified in the method's signature, or from a class that extends that class (i.e., a subclass of that class).
给出一个合适的通用功能,方法是指定在调用setMethod由通常的方式。期待“相应的所有参数定义的方法必须写......”是从该方法的签名中指定的类,或从一个类,扩展这个类(即,这个类的一个子类)。

Typically the methods will pass “...” down to another function or will create a list of the arguments and iterate over that.  See the examples below.
通常的方法将通过“...”到另一个功能或将创建一个参数列表和遍历,。见下面的例子。

When you have a computation that is suitable for more than one existing class, a convenient approach may be to define a union of these classes by a call to setClassUnion. See the example below.
当你有一个方便的方法计算,是适合多个现有类,可能是定义调用setClassUnion由这些类的工会。请看下面的例子。


方法的选择,并派遣“......”----------Method Selection and Dispatch for "..."----------

See Methods for a general discussion.  The following assumes you have read the “Method Selection and Dispatch” section of that documentation.
看到了一般性讨论的方法。以下假定您已阅读“方法的选择和调度”该文件第。

A method selecting on “...” is specified by a single class in the call to setMethod.  If all the actual arguments corresponding to “...” have this class, the corresponding method is selected directly.
“......”是由单个类调用setMethod在指定选择的方法。如果所有对应的实际参数“......”有这个类,选择相应的方法直接。

Otherwise, the class of each argument and that class' superclasses are computed, beginning with the first “...” argument.  For the first argument, eligible methods are those for any of the classes.   For each succeeding argument that introduces a class not considered previously, the eligible methods are further restricted to those matching the argument's class or superclasses. If no further eligible classes exist, the iteration breaks out and the default method, if any, is selected.
否则,计算每个参数,类的父类,第一起“......”的说法。为第一个参数,合资格的方法的任何类。对于每一个成功的论点,即引入以前没有考虑一类,合资格的方法,进一步限制那些匹配的参数的类或超类。如果没有进一步的资格类存在,的迭代打破了默认的方法,如果有的话,被选中。

At the end of the iteration, one or more methods may be eligible. If more than one, the selection looks for the method with the least distance to the actual arguments.  For each argument, any inherited method corresponds to a distance, available from the contains slot of the class definition.  Since the same class can arise for more than one argument, there may be several distances associated with it.  Combining them is inevitably arbitrary:  the current computation uses the minimum distance.  Thus, for example, if a method matched one argument directly, one as  first generation superclass and another as a second generation superclass, the distances are 0, 1 and 2.  The current selection computation would use distance 0 for this method.  In particular, this selection criterion tends to use a method that matches exactly one or more of the arguments' class.
在迭代结束时,一个或多个方法可能有资格。如果不止一个,选择看起来与实际参数的最小距离的方法。对于每个参数,任何继承的方法对应距离contains类定义的槽。由于同一类的多个参数可能会出现,有可能是与它相关的几个距离。他们结合是不可避免的任意使用当前计算的最小距离。因此,举例来说,如果一个方法匹配一个参数,直接作为第一代超类作为第二代超,距离是0,1和2。当前选择的计算,使用这种方法距离0。本次评选的标准,特别是倾向于使用的方法相匹配的一个或更多的参数的类。

As with ordinary method selection, there may be multiple methods with the same distance.  A warning  message is issued and one of the methods is chosen (the first encountered, which in this case is rather arbitrary).
至于用普通方法的选择,也有可能是多个方法具有相同的距离。发出一个警告信息和选择的方法之一(第一次遇到,在这种情况下是相当武断的)。

Notice that, while the computation examines all arguments, the essential cost of dispatch goes up with the number of distinct classes among the arguments, likely to be much smaller than the number of arguments when the latter is large.
请注意,计算,同时检查所有参数,派遣必要的成本上升与不同类别之间的参数,可能要比参数的数目时,后者则是大是小数目。


实施细则----------Implementation Details----------

Methods dispatching on “...” were introduced in version 2.8.0 of R.  The initial implementation of the corresponding selection and dispatch is in an R function, for flexibility while the new mechanism is being studied.  In this implementation, a local version of setGeneric is inserted in the generic function's environment.  The local version selects a method according to the criteria above and calls that method, from the environment of the generic function.  This is slightly different from the action taken by the C implementation when “...” is not involved.  Aside from the extra computing time required, the method is evaluated in a true function call, as opposed to the special context constructed by the C version (which cannot be exactly replicated in R code.)  However, situations in which different computational results would be obtained have not been encountered so far, and seem very unlikely.
方法调度“......”,介绍了在2.8.0版本的河初步实施相应的选择和调度是在一个R函数的灵活性,同时正在研究新的机制。在此实施,本地版本的setGeneric插入的通用功能的环境中。本地版本选择的方法,根据上述标准和方法,要求从环境的通用功能。这是由C执行时“......”不涉及所采取的行动略有不同。除了在额外的计算时间要求,方法评价相,特殊情况下由C版本兴建一个真正的函数调用(它可以不被完全R代码复制)然而,在哪些情况下不同的计算结果将是没有遇到到目前为止,似乎不太可能。

Methods dispatching on arguments other than “...” are cached by storing the inherited method in the table of all methods, where it will be found on the next selection with the same combination of classes in the actual arguments (but not used for inheritance searches). Methods based on “...” are also cached, but not found quite as immediately.  As noted, the selected method depends only on the set of classes that occur in the “...” arguments.  Each of these classes can appear one or more times, so many combinations of actual argument classes will give rise to the same effective signature.  The selection computation first computes and sorts the distinct classes encountered.  This gives a label that will be cached in the table of all methods, avoiding any further search for inherited classes after the first occurrence.  A call to showMethods will expose such inherited methods.
调度参数以外的“...”的方法是缓存存储在表的所有方法,它会被发现对未来的选择,在实际参数与类相同的组合(但不使用继承的方法继承搜索)。方法基础上“......”也被缓存,但没有找到相当,立刻。如上所述,选定的方法只依赖于“...”的论点出现的一组类。这些类中的每个人都可以出现一次或多次,所以许多实际参数类的组合会产生相同的有效签名。选择计算首先计算和遇到的各种各样的不同的类。这给出了一个标签,这将是缓存表中的所有方法,避免进一步搜索后第一次出现的任何继承类。一个showMethods调用会揭露这种继承的方法。

The intention is that the “...” features will be added to the standard C code when enough experience with them has been obtained. It is possible that at the same time, combinations of “...” with other arguments in signatures may be supported.
其用意是,“...”功能将被添加到标准的C代码时,已获得足够的经验与他们。这是可能的,在同一时间,“...”签名中的其他参数的组合,可支持。


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

Software for Data Analysis: Programming with R Springer.  (For the R version.)
Programming with Data Springer (For the original S4 version.)

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

For the general discussion of methods, see  Methods and links from there.
对于一般性讨论的方法,从那里看到的方法和链接。


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


cc <- function(...)c(...)

setGeneric("cc")

setMethod("cc", "character", function(...)paste(...))

setClassUnion("Number", c("numeric", "complex"))

setMethod("cc", "Number", function(...) sum(...))

setClass("cdate", contains = "character", representation(date = "Date"))

setClass("vdate", contains = "vector", representation(date = "Date"))

cd1 <- new("cdate", "abcdef", date = Sys.Date())

cd2 <- new("vdate", "abcdef", date = Sys.Date())

stopifnot(identical(cc(letters, character(), cd1),
           paste(letters, character(), cd1))) # the "character" method[“人物”的方法]

stopifnot(identical(cc(letters, character(), cd2),
                    c(letters, character(), cd2)))
# the default, because "vdate" doesn't extend "character"[默认情况下,因为“vdate”不延长“字符”]

stopifnot(identical(cc(1:10, 1+1i), sum(1:10, 1+1i))) # the "Number" method[“数”的方法]

stopifnot(identical(cc(1:10, 1+1i, TRUE), c(1:10, 1+1i, TRUE))) # the default[默认]

stopifnot(identical(cc(), c())) # no arguments implies the default method[没有参数,意味着默认的方法]

setGeneric("numMax", function(...)standardGeneric("numMax"))

setMethod("numMax", "numeric", function(...)max(...))
# won't work for complex data[为复杂的数据将无法正常工作]
setMethod("numMax", "Number", function(...) paste(...))
# should not be selected w/o complex args[不应选择W / O复杂ARGS]

stopifnot(identical(numMax(1:10, pi, 1+1i), paste(1:10, pi, 1+1i)))
stopifnot(identical(numMax(1:10, pi, 1), max(1:10, pi, 1)))

try(numMax(1:10, pi, TRUE)) # should be an error:  no default method[应该是一个错误:没有默认的方法]

## A generic version of paste(), dispatching on the "..." argument:[#通用版本糊(),调度上的“...”参数:]
setGeneric("paste", signature = "...")

setMethod("paste", "Number", function(..., sep, collapse) c(...))

stopifnot(identical(paste(1:10, pi, 1), c(1:10, pi, 1)))



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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-23 12:12 , Processed in 0.022669 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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