setMethod(methods)
setMethod()所属R语言包:methods
Create and Save a Method
创建和保存方法
译者:生物统计家园网 机器人LoveR
描述----------Description----------
Create and save a formal method for a given function and list of classes.
创建并保存为一个给定的函数和类名单的正式方法。
用法----------Usage----------
setMethod(f, signature=character(), definition,
where = topenv(parent.frame()),
valueClass = NULL, sealed = FALSE)
removeMethod(f, signature, where)
参数----------Arguments----------
参数:f
A generic function or the character-string name of the function.
一个泛型函数或函数的字符的字符串名称。
参数:signature
A match of formal argument names for f with the character-string names of corresponding classes. See the details below; however, if the signature is not trivial, you should use method.skeleton to generate a valid call to setMethod.
正式参数名匹配f字符的字符串名称与相应的类。看到下面的细节;然而,如果签名是不平凡的,你应该使用method.skeleton生成一个有效的电话setMethod。
参数:definition
A function definition, which will become the method called when the arguments in a call to f match the classes in signature, directly or through inheritance.
一个函数的定义,这将成为当在通话中的参数f匹配signature,直接或通过继承类的方法调用。
参数:where
the environment in which to store the definition of the method. For setMethod, it is recommended to omit this argument and to include the call in source code that is evaluated at the top level; that is, either in an R session by something equivalent to a call to source, or as part of the R source code for a package. For removeMethod, the default is the location of the (first) instance of the method for this signature.
环境在其中存储的方法定义。 setMethod,建议省略此参数,并在顶层评估包括在源代码中调用,也就是说,无论是在R会话,相当于到source的调用由东西或r源代码包的一部分。 removeMethod,默认的是在此签名的方法(一)实例的位置。
参数:valueClass
Obsolete and unused, but see the same argument for setGeneric.
过时的和未使用的,但看到的setGeneric同样的论点。
参数:sealed
If TRUE, the method so defined cannot be redefined by another call to setMethod (although it can be removed and then re-assigned).
TRUE如果,所以定义的方法不能被重新定义setMethod(虽然它可以被删除,然后重新分配)调用另一个。
Details
详情----------Details----------
The call to setMethod stores the supplied method definition in the metadata table for this generic function in the environment, typically the global environment or the namespace of a package. In the case of a package, the table object becomes part of the namespace or environment of the package. When the package is loaded into a later session, the methods will be merged into the table of methods in the corresponding generic function object.
setMethod呼叫存储在元数据表提供的方法定义这个环境中的通用功能,通常全球环境或包的命名空间。表对象在一个包的情况下,成为包的命名空间或环境的一部分。当包加载到以后的会议中,该方法将被合并到相应的通用功能对象的方法表。
Generic functions are referenced by the combination of the function name and the package name; for example, the function "show" from the package "methods". Metadata for methods is identified by the two strings; in particular, the generic function object itself has slots containing its name and its package name. The package name of a generic is set according to the package from which it originally comes; in particular, and frequently, the package where a non-generic version of the function originated. For example, generic functions for all the functions in package base will have "base" as the package name, although none of them is an S4 generic on that package. These include most of the base functions that are primitives, rather than true functions; see the section on primitive functions in the documentation for setGeneric for details.
泛型函数引用的函数的名称和包名称的组合,例如,该功能包"show""methods"。元数据的方法确定两个字符串,尤其是通用的函数对象本身已插槽包含其名称和包名称。根据包从它最初来自一个通用的软件包名称设置;,特别是频繁,起源的非泛型版本的功能包。例如,在包base的所有功能的通用功能将有"base"作为包名,虽然他们都不是在该包中S4中通用。这些措施包括大部分的基本功能是原语,而不是真正的功能;看到文档中的原始功能为setGeneric详情。
Multiple packages can have methods for the same generic function; that is, for the same combination of generic function name and package name. Even though the methods are stored in separate tables in separate environments, loading the corresponding packages adds the methods to the table in the generic function itself, for the duration of the session.
多个包可以有相同的通用功能的方法,这就是泛型函数的名称和包名称相同的组合。即使方法是单独的表存储在不同的环境中,加载相应的软件包的方法添加泛型函数本身的表,在会议期间。
The class names in the signature can be any formal class, including basic classes such as "numeric", "character", and "matrix". Two additional special class names can appear: "ANY", meaning that this argument can have any class at all; and "missing", meaning that this argument must not appear in the call in order to match this signature. Don't confuse these two: if an argument isn't mentioned in a signature, it corresponds implicitly to class "ANY", not to "missing". See the example below. Old-style ("S3") classes can also be used, if you need compatibility with these, but you should definitely declare these classes by calling setOldClass if you want S3-style inheritance to work.
在签名的类名可以是任何正式的类,包括基本的类,如"numeric","character","matrix"。另外两个特殊的类名可以出现:"ANY",也就是说,这个参数可以有任何类;"missing",也就是说,这种说法不能出现在通话中,为了配合这个签名。不要混淆这两个:如果没有在签名中提到一个论点是,它对应隐式类"ANY"不"missing"。请看下面的例子。旧式(S3的)类,也可以被使用,如果您需要这些兼容性,但你一定要申报这些类通过调用setOldClass如果你想与S3风格的继承工作。
Method definitions can have default expressions for arguments, but a current limitation is that the generic function must have some default expression for the same argument in order for the method's defaults to be used. If so, and if the corresponding argument is missing in the call to the generic function, the default expression in the method is used. If the method definition has no default for the argument, then the expression supplied in the definition of the generic function itself is used, but note that this expression will be evaluated using the enclosing environment of the method, not of the generic function. Note also that specifying class "missing" in the signature does not require any default expressions, and method selection does not evaluate default expressions. All actual (non-missing) arguments in the signature of the generic function will be evaluated when a method is selected—when the call to standardGeneric(f) occurs.
方法定义可以有参数的默认表达式,但当前的限制是,通用的功能必须有一些以相同的使用方法的默认参数的默认表达。如果是这样,如果在泛型函数调用中缺少相应的参数,在方法的默认表达式使用。如果方法定义的参数没有默认值,然后使用泛型函数本身的定义中提供的表达,但请注意,这个表达式将被评估了该方法的封闭环境中使用,而不是泛型函数。还请注意,指定类"missing"签名并不需要任何预设表达式,和方法的选择不计算默认表达式。一种方法是选择时调用standardGeneric(f)发生时,将评估所有实际(非丢失)泛型函数的签名中的参数。
It is possible to have some differences between the formal arguments to a method supplied to setMethod and those of the generic. Roughly, if the generic has ... as one of its arguments, then the method may have extra formal arguments, which will be matched from the arguments matching ... in the call to f. (What actually happens is that a local function is created inside the method, with the modified formal arguments, and the method is re-defined to call that local function.)
这是可能的,有以提供setMethod和通用的方法正式参数之间的一些分歧。粗略地说,如果通用有...作为它的参数之一,则该方法可能有多余的形式参数,将匹配的参数匹配...呼叫f。 (实际情况是,创建一个局部函数里面的方法,与修改后的正式参数,方法是重新定义调用该本地函数。)
Method dispatch tries to match the class of the actual arguments in a call to the available methods collected for f. If there is a method defined for the exact same classes as in this call, that method is used. Otherwise, all possible signatures are considered corresponding to the actual classes or to superclasses of the actual classes (including "ANY"). The method having the least distance from the actual classes is chosen; if more than one method has minimal distance, one is chosen (the lexicographically first in terms of superclasses) but a warning is issued. All inherited methods chosen are stored in another table, so that the inheritance calculations only need to be done once per session per sequence of actual classes. See Methods for more details.
方法调度,试图在通话中的实际参数匹配类f收集可用的方法,。如果有在此调用完全相同类中定义的方法,该方法使用。否则,所有可能的签名被认为是对应于实际的类或实际的类的超类(包括"ANY")。从实际类的最小距离的方法是选择,如果有多个方法的最小距离,选择(父字典第一),但会发出一个警告。存储在另一个表中选择所有继承的方法,使计算的继承只需要实际类的顺序进行,每节,每一次。看到更多细节的方法。
The function removeMethod removes the specified method from the metadata table in the corresponding environment. It's not a function that is used much, since one normally wants to redefine a method rather than leave no definition.
功能removeMethod元数据表中相应的环境中删除指定的方法。它不是一个用于非常重要的作用,因为人们通常要重新定义一个方法,而不是留下任何定义。
值----------Value----------
These functions exist for their side-effect, in setting or removing a method in the object defining methods for the specified generic.
这些功能,其副作用存在,设置或删除一个对象的方法定义为指定的通用方法。
The value returned by removeMethod is TRUE if a method was found to be removed.
返回的值removeMethod是TRUE如果一个方法被发现将被删除。
参考文献----------References----------
Software for Data Analysis: Programming with R Springer. (For the R version.)
Programming with Data Springer (For the original S4 version.)
参见----------See Also----------
method.skeleton, which is the recommended way to generate a skeleton of the call to setMethod, with the correct formal arguments and other details.
method.skeleton,这是推荐的方式,以正确的形式参数和其他细节,生成的调用setMethod骨架。
Methods and the links there for a general discussion, dotsMethods for methods that dispatch on “...”, and setGeneric for generic functions.
方法和存在的联系进行了一般性讨论,dotsMethods派遣“...”的方法,setGeneric通用功能。
举例----------Examples----------
require(graphics)
## methods for plotting track objects (see the example for \link{setClass})[#图跟踪对象的方法(见链接\ {setClass}例如)]
##[#]
## First, with only one object as argument:[#首先,只有一个对象作为参数:]
setMethod("plot", signature(x="track", y="missing"),
function(x, y, ...) plot(slot(x, "x"), slot(x, "y"), ...)
)
## Second, plot the data from the track on the y-axis against anything[#第二,从轨道上绘制Y轴反对任何数据]
## as the x data.[#x数据。]
setMethod("plot", signature(y = "track"),
function(x, y, ...) plot(x, slot(y, "y"), ...)
)
## and similarly with the track on the x-axis (using the short form of[#同样与x轴使用缩写形式(轨道]
## specification for signatures)[#规范的签名)]
setMethod("plot", "track",
function(x, y, ...) plot(slot(x, "y"), y, ...)
)
t1 <- new("track", x=1:20, y=(1:20)^2)
tc1 <- new("trackCurve", t1)
slot(tc1, "smooth") <- smooth.spline(slot(tc1, "x"), slot(tc1, "y"))$y #$[$]
plot(t1)
plot(qnorm(ppoints(20)), t1)
## An example of inherited methods, and of conforming method arguments[#继承的方法的一个例子,符合方法参数]
## (note the dotCurve argument in the method, which will be pulled out[#(注意方法dotCurve的说法,这将被拉出]
## of ... in the generic.[排名第...在通用。]
setMethod("plot", c("trackCurve", "missing"),
function(x, y, dotCurve = FALSE, ...) {
plot(as(x, "track"))
if(length(slot(x, "smooth") > 0))
lines(slot(x, "x"), slot(x, "smooth"),
lty = if(dotCurve) 2 else 1)
}
)
## the plot of tc1 alone has an added curve; other uses of tc1[#TC1单独的图有一个附加的曲线; TC1其他用途]
## are treated as if it were a "track" object.[#被视为是“跟踪”对象。]
plot(tc1, dotCurve = TRUE)
plot(qnorm(ppoints(20)), tc1)
## defining methods for a special function.[#定义一个特殊功能的方法。]
## Although "[" and "length" are not ordinary functions[“[”和“长度”#虽然不是普通的函数]
## methods can be defined for them.[它们可以被定义为#方法。]
setMethod("[", "track",
function(x, i, j, ..., drop) {
x@x <- x@x[i]; x@y <- x@y[i]
x
})
plot(t1[1:15])
setMethod("length", "track", function(x)length(x@y))
length(t1)
## methods can be defined for missing arguments as well[#方法可以被定义为缺少参数,以及]
setGeneric("summary") ## make the function into a generic[#使之成为一个通用的函数]
## A method for summary()[#方法(摘要)]
## The method definition can include the arguments, but[#定义的方法可以包括参数,但]
## if they're omitted, class "missing" is assumed.[#如果他们省略,类“失踪”的假设。]
setMethod("summary", "missing", function() "<No Object>")
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|