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

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

[复制链接]
发表于 2012-2-16 19:15:56 | 显示全部楼层 |阅读模式
UseMethod(base)
UseMethod()所属R语言包:base

                                        Class Methods
                                         类的方法

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

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

R possesses a simple generic function mechanism which can be used for an object-oriented style of programming.  Method dispatch takes place based on the class(es) of the first argument to the generic function or of the object supplied as an argument to UseMethod or NextMethod.
ŕ拥有一个面向对象的编程风格可以使用一个简单的通用功能的机制。基于泛型函数类的第一个参数(ES)或作为UseMethod或NextMethod的参数提供的对象,方法调度发生。


用法----------Usage----------


UseMethod(generic, object)

NextMethod(generic = NULL, object = NULL, ...)



参数----------Arguments----------

参数:generic
a character string naming a function (and not a built-in operator).  Required for UseMethod.
一个字符串,命名一个函数(而不是一个内置运营商)。需要UseMethod。


参数:object
for UseMethod: an object whose class will determine the method to be dispatched.  Defaults to the first argument of the enclosing function.
UseMethod“:它的类的对象将确定被派遣的方法。封闭函数的第一个参数的默认值。


参数:...
further arguments to be passed to the next method.
进一步的参数被传递到下一个方法。


Details

详情----------Details----------

An R object is a data object which has a class attribute (and this can be tested by is.object). A class attribute is a character vector giving the names of the classes from which the object inherits. If the object does not have a class attribute, it has an implicit class.  Matrices and arrays have class "matrix" or"array" followed by the class of the underlying vector. Most vectors have class the result of mode(x), except that integer vectors have class c("integer", "numeric") and real vectors have class c("double", "numeric").
一个R对象是一个数据对象,它有一个class属性(这可以测试is.object)。 A类属性是一个特征向量,从该对象继承的类的名称。如果该对象没有属性类,它有一个隐含的类。矩阵和数组类"matrix"或"array"类遵循的基本向量。大多数向量有类mode(x)的结果,除了整数向量有类c("integer", "numeric")类c("double", "numeric")和现实的向量。

When a function calling UseMethod("fun") is applied to an object with class attribute c("first", "second"), the system searches for a function called fun.first and, if it finds it, applies it to the object.  If no such function is found a function called fun.second is tried.  If no class name produces a suitable function, the function fun.default is used, if it exists, or an error results.
当一个函数调用UseMethod("fun")被应用到一个对象类的属性c("first", "second"),称为fun.first“,如果它发现它的功能的系统搜索,适用于它的对象。如果没有这样的功能被发现名为fun.second的功能尝试。如果没有类名产生一个合适的函数,函数fun.default使用,如果它存在,或错误的结果。

Function methods can be used to find out about the methods for a particular generic function or class.
功能methods可以用来寻找一个特定的通用函数或类方法。

UseMethod is a primitive function but (as from R 2.11.0) uses standard argument matching.  It is not the only means of dispatch of methods, for there are internal generic and group generic functions.  UseMethod currently dispatches on the implicit class even for arguments that are not objects, but the other means of dispatch do not.
UseMethod是一种原始的功能,但(从R 2.11.0)使用标准的参数匹配。这不是调度方法的唯一手段,有内部的通用和通用功能组。 UseMethod目前隐类,甚至不是对象的参数,但派遣其他手段的调度做。

NextMethod invokes the next method (determined by the class vector, either of the object supplied to the generic, or of the first argument to the function containing NextMethod if a method was invoked directly).  Normally NextMethod is used with only one argument, generic, but if further arguments are supplied these modify the call to the next method.
NextMethod调用下一个方法(类向量决定,无论是通用的,或含有NextMethod如果一个方法是直接调用的函数的第一个参数提供的对象)。通常NextMethod只有一个参数,generic,但如果提供进一步的论据是,这些修改下一个方法调用。

NextMethod should not be called except in methods called by UseMethod or from internal generics (see InternalGenerics).  In particular it will not work inside anonymous calling functions (e.g. get("print.ts")(AirPassengers)).
NextMethod应该不能除外称为UseMethod或从内部泛型(见InternalGenerics)的方法调用。特别是,它不会匿名调用函数内的工作(例如get("print.ts")(AirPassengers))。

Namespaces can register methods for generic functions.  To support this, UseMethod and NextMethod search for methods in two places: first in the environment in which the generic function is called, and then in the registration data base for the environment in which the generic is defined (typically a namespace). So methods for a generic function need to be available in the environment of the call to the generic, or they must be registered. (It does not matter whether they are visible in the environment in which the generic is defined.)
命名空间可以注册通用功能的方法。为了支持这项工作,UseMethod和NextMethod搜索方法:在两个地方的环境中的通用功能被称为第一,在对环境的登记数据的基础上,然后在通用的定义(通常是一个命名空间)。因此,一个泛型函数方法需要在调用通用的环境,他们必须注册。 (这并不无论他们是在通用定义的环境中可见。)


技术细节----------Technical Details----------

Now for some obscure details that need to appear somewhere.  These comments will be slightly different than those in Chambers(1992). (See also the draft "R Language Definition".) UseMethod creates a new function call with arguments matched as they came in to the generic.  Any local variables defined before the call to UseMethod are retained (unlike S).  Any statements after the call to UseMethod will not be evaluated as UseMethod does not return.  UseMethod can be called with more than two arguments: a warning will be given and additional arguments ignored.  (They are not completely ignored in S.)  If it is called with just one argument, the class of the first argument of the enclosing function is used as object: unlike S this is the first actual argument passed and not the current value of the object of that name.
现在需要的地方出现的一些不起眼的细节。这些意见将是比钱伯斯(1992)略有不同。 (亦见“草案”R语言的定义“。)UseMethod创建一个新的函数调用与参数匹配,因为他们在通用。任何定义调用UseMethod前的局部变量保留(不像)调用UseMethod不会UseMethod不返回评估后的任何陈述。 UseMethod可以有两个以上的参数称为:警告将给予额外的参数被忽略。 (他们不完全在S忽略),如果它只有一个参数调用,object:不像s这是第一个实际参数传递,而不是封闭函数的第一个参数的类被用作该名称的对象的当前值。

NextMethod works by creating a special call frame for the next method.  If no new arguments are supplied, the arguments will be the same in number, order and name as those to the current method but their values will be promises to evaluate their name in the current method and environment.  Any named arguments matched to ... are handled specially: they either replace existing arguments of the same name or are appended to the argument list.  They are passed on as the promise that was supplied as an argument to the current environment.  (S does this differently!)  If they have been evaluated in the current (or a previous environment) they remain evaluated. (This is a complex area, and subject to change: see the draft "R Language Definition".)
NextMethod工作,通过创建一个特殊的下一个方法调用帧。如果没有提供新的论据,论点将是相同的,到目前的方法,但他们的价值观,将他们的名字,以评估目前的方法和环境的承诺,在数量,顺序和名称。任何命名参数匹配...特殊处理:他们要么取代现有的同名参数或追加到参数列表。他们传递的承诺,作为当前环境的参数提供。 (不同),如果他们已在当前(或以前的环境),他们仍然评估评估。 (这是一个复杂的领域,如有变更,见草案“R语言定义。)

The search for methods for NextMethod is slightly different from that for UseMethod.   Finding no fun.default is not necessarily an error, as the search continues to the generic itself.  This is to pick up an internal generic like [ which has no separate default method, and succeeds only if the generic is a primitive function or a wrapper for a .Internal function of the same name.  (When a primitive is called as the default method, argument matching may not work as described above due to the different semantics of primitives.)
为搜索NextMethod的方法是UseMethod略有不同。没有fun.default不一定是一个错误,继续搜索通用本身。这是内部通用拿起一个像[有没有单独的默认方法,成功的只有通用,是一种原始的功能或包装.Internal同名的函数。 (当一个原始的默认方法调用,参数匹配可能无法正常工作如上所述由于不同的语义原语。)

You will see objects such as .Generic, .Method, and .Class used in methods.  These are set in the environment within which the method is evaluated by the dispatch mechanism, which is as follows:
你会看到如.Generic,.Method,.Class方法中使用的对象。这些设置在环境内,该方法评估的调度机制,这是如下:

Find the context for the calling function (the generic): this gives us the unevaluated arguments for the original call.
查找调用函数的上下文(通用):这给了我们原来的通话不计算参数。

Evaluate the object (usually an argument) to be used for dispatch, and find a method (possibly the default method) or throw an error.
评估的对象(通常是一个参数)调度使用,并找到一种方法(可能是默认的方法),或抛出一个错误。

Create an environment for evaluating the method and insert special variables (see below) into that environment.  Also copy any variables in the environment of the generic that are not formal (or actual) arguments.
创建环境评估方法和插入到该环境中的特殊变量(见下文)。还可以复制任何不正规(或实际)参数的通用环境变量。

Fix up the argument list to be the arguments of the call matched to the formals of the method.
修正参数列表匹配的方法的形参调用的参数。

.Generic is a length-one character vector naming the generic function.
.Generic是一个长度为一个字符命名的通用功能的向量。

.Method is a character vector (normally of length one) naming the method function.  (For functions in the group generic Ops it is of length two.)
.Method是一个特征向量(通常长度为一)命名方法的功能。 (组中的功能,通用Ops长度为二。)

.Class is a character vector of classes used to find the next method.  NextMethod adds an attribute "previous" to .Class giving the .Class last used for dispatch, and shifts .Class along to that used for dispatch.
.Class是一个用来寻找下一个方法的类的特征向量。 NextMethod添加一个属性"previous"给.Class最后调度使用.Class移动.Class沿调度使用。

.GenericCallEnv and .GenericDefEnv are the environments of the call to be generic and defining the generic respectively.  (The latter is used to find methods registered for the generic.)
.GenericCallEnv和.GenericDefEnv通话的环境,是通用和通用分别定义。 (后者用于找到注册的通用的方法。)

Note that .Class is set when the generic is called, and is unchanged if the class of the dispatching argument is changed in a method.  It is possible to change the method that NextMethod would dispatch by manipulating .Class, but "this is not recommended unless you understand the inheritance mechanism thoroughly" (Chambers & Hastie, 1992, p. 469).
注意.Class设置时,被称为通用,是不变的,如果调度参数的类方法改变。它是可以改变的方法NextMethod将派遣由操纵.Class,但“这是不推荐,除非你了解彻底的继承机制(钱伯斯和哈斯蒂,1992年,469) 。


注意----------Note----------

This scheme is called S3 (S version 3).  For new projects, it is recommended to use the more flexible and robust S4 scheme provided in the methods package.
这项计划被称为S3(第3版)。对于新的项目,建议使用更加灵活和强大的中S4中methods包中提供的计划。


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

Classes and methods: object-oriented programming in S. Appendix A of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.

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

The draft "R Language Definition".
草案“R语言的定义”。

methods, class, getS3method, is.object.
methods,class,getS3method,is.object。

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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-24 13:31 , Processed in 0.022466 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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