GenericFunctions(methods)
GenericFunctions()所属R语言包:methods
Tools for Managing Generic Functions
管理通用功能的工具
译者:生物统计家园网 机器人LoveR
描述----------Description----------
The functions documented here manage collections of methods associated with a generic function, as well as providing information about the generic functions themselves.
这里记录的功能,管理一个泛型函数的方法的集合,以及提供有关通用函数本身的信息。
用法----------Usage----------
isGeneric(f, where, fdef, getName = FALSE)
isGroup(f, where, fdef)
removeGeneric(f, where)
dumpMethod(f, signature, file, where, def)
findFunction(f, generic = TRUE, where = topenv(parent.frame()))
dumpMethods(f, file, signature, methods, where)
signature(...)
removeMethods(f, where = topenv(parent.frame()), all = missing(where))
setReplaceMethod(f, ..., where = topenv(parent.frame()))
getGenerics(where, searchForm = FALSE)
参数----------Arguments----------
参数:f
The character string naming the function.
字符串命名的功能。
参数:where
The environment, namespace, or search-list position from which to search for objects. By default, start at the top-level environment of the calling function, typically the global environment (i.e., use the search list), or the namespace of a package from which the call came. It is important to supply this argument when calling any of these functions indirectly. With package namespaces, the default is likely to be wrong in such calls.
环境,空间,或从中寻找对象的搜索列表中的位置。默认情况下,开始在顶层调用函数,通常是全球性的环境(即,使用搜索列表中),或从其中调用了一个包的命名空间环境。当调用这些函数的任何间接的,重要的是要提供这种说法。包的命名空间,默认的是在这样的调用可能会错误。
参数:signature
The class signature of the relevant method. A signature is a named or unnamed vector of character strings. If named, the names must be formal argument names for the generic function. Signatures are matched to the arguments specified in the signature slot of the generic function (see the Details section of the setMethod documentation). The signature argument to dumpMethods is ignored (it was used internally in previous implementations).
有关方法的类的签名。签名是一个有名或无名的字符串向量。如果命名,名称必须是正式的通用函数的参数名。签名匹配到签名插槽的通用功能(看到setMethod文件的详细信息部分)在指定的参数。 signaturedumpMethods参数将被忽略(在以前的实现,它是用于内部)。
参数:file
The file or connection on which to dump method definitions.
文件或连接上倾倒的方法定义。
参数:def
The function object defining the method; if omitted, the current method definition corresponding to the signature.
函数对象的确定方法;如果省略,当前的方法定义相应的签名。
参数:...
Named or unnamed arguments to form a signature.
命名或无名的参数,以形成一个签名。
参数:generic
In testing or finding functions, should generic functions be included. Supply as FALSE to get only non-generic functions.
在测试或寻找功能,通用功能包括在内。供应FALSE只得到非通用功能。
参数:fdef
Optional, the generic function definition. Usually omitted in calls to isGeneric
可选的,通用的功能定义。通常省略isGeneric电话
参数:getName
If TRUE, isGeneric returns the name of the generic. By default, it returns TRUE.
如果TRUE,isGeneric返回的通用名称。默认情况下,它返回TRUE。
参数:methods
The methods object containing the methods to be dumped. By default, the methods defined for this generic (optionally on the specified where location).
该方法的对象包含的方法倾倒。默认情况下,这个通用(可选指定where位置)的方法定义。
参数:all
in removeMethods, logical indicating if all (default) or only the first method found should be removed.
removeMethods,逻辑说明(默认),或仅发现的第一种方法应该被删除。
参数:searchForm
In getGenerics, if TRUE, the package slot of the returned result is in the form used by search(), otherwise as the simple package name (e.g, "package:base" vs "base").
在getGenerics如果TRUE,package槽返回的结果是由search()使用的形式,否则简单的包名(例如,"package:base" 与"base")。
功能汇总----------Summary of Functions----------
Is there a function named f, and if so, is it a generic?
是有一个函数名为f,如果是这样,它是一个通用的吗?
The getName argument allows a function to find the name from a function definition. If it is TRUE then the name of the generic is returned, or FALSE if this is not a generic function definition.
getName参数允许一个函数,从函数的定义名称。如果是的话TRUE的通用名称,则返回,或FALSE如果这不是一个通用的函数定义。
The behavior of isGeneric and getGeneric for primitive functions is slightly different. These functions don't exist as formal function objects (for efficiency and historical reasons), regardless of whether methods have been defined for them. A call to isGeneric tells you whether methods have been defined for this primitive function, anywhere in the current search list, or in the specified position where. In contrast, a call to getGeneric will return what the generic for that function would be, even if no methods have been currently defined for it.
的行为isGeneric和getGeneric是略有不同的原始功能。这些功能不存在正式的函数对象(效率和历史的原因),不论是否已为它们定义方法。一个isGeneric调用告诉你方法是否已经被定义为这原始的功能,当前的搜索列表中的任何位置,或在指定的位置where。相比之下通话,getGeneric的将返回该功能的通用将是什么,即使目前没有方法已为它定义。
Remove all the methods for the generic function of this name. In addition, removeGeneric removes the function itself; removeMethods restores the non-generic function which was the default method. If there was no default method, removeMethods leaves a generic function with no methods.
删除这个名字的通用功能的所有方法。此外,removeGeneric删除函数本身;removeMethods恢复非通用功能,这是默认的方法。如果没有默认的方法,removeMethods留下一个没有方法的通用功能。
Dispatches a method from the current function call for the generic function f. It is an error to call standardGeneric anywhere except in the body of the corresponding generic function.
调度一个泛型函数f从当前函数调用的方法。这是一个错误,调用standardGeneric任何地方,除了在身体相应的通用功能。
Note that standardGeneric is a primitive function in the base package for efficiency reasons, but rather documented here where it belongs naturally.
注意standardGeneric是base包在原始的功能效率的原因,而是记录在这里,它属于自然的。
Dump the method for this generic function and signature.
转储这个泛型函数和签名的方法。
return a list of either the positions on the search list, or the current top-level environment, on which a function object for name exists. The returned value is always a list, use the first element to access the first visible version of the function. See the example.
返回的搜索列表中的位置列表,或目前的顶级环境,为name函数对象存在。返回值始终是一个列表,使用的第一个元素访问的第一个可见的功能版本。见的例子。
NOTE: Use this rather than find with mode="function", which is not as meaningful, and has a few subtle bugs from its use of regular expressions. Also, findFunction works correctly in the code for a package when attaching the package via a call to library.
注:使用此而不是find与mode="function",这是不是有意义,有一些细微的错误,从它的使用正则表达式。也,findFunction正常工作时附加包调用library通过包中的代码。
Dump all the methods for this generic.
转储这个通用方法。
Returns a named list of classes to be matched to arguments of a generic function.
返回一个类的命名要匹配一个泛型函数的参数列表。
getGenerics: returns the names of the generic functions that have methods defined on where; this argument can be an environment or an index into the search list. By default, the whole search list is used.
getGenerics:返回上定义的方法的通用功能的名称where;这种说法,可以到搜索列表中的环境或索引。默认情况下,整个搜索列表。
The methods definitions are stored with package qualifiers; for example, methods for function "initialize" might refer to two different functions of that name, on different packages. The package names corresponding to the method list object are contained in the slot package of the returned object. The form of the returned name can be plain (e.g., "base"), or in the form used in the search list ("package:base")
包限定的方法定义存储,例如,函数"initialize"的方法可能是指这个名字,在不同的包,两种不同的功能。包名对应的方法列表中的对象都包含在插槽package返回的对象。返回名称的形式可以是纯的(例如,"base"),或在搜索列表中使用的形式("package:base")
Details
详情----------Details----------
If there is already a non-generic function of this name, it will be used to define the generic unless def is supplied, and the current function will become the default method for the generic.
如果有这个名字已经是一个非泛型的功能,它会被用来定义通用除非def提供和目前功能将成为默认的通用方法。
If def is supplied, this defines the generic function, and no default method will exist (often a good feature, if the function should only be available for a meaningful subset of all objects).
如果def提供,这个定义通用的功能,并没有预设的方法,将存在(通常是一个很好的功能,如果该函数只应是有意义的子集的所有对象)。
Arguments group and valueClass are retained for consistency with S-Plus, but are currently not used.
参数group和valueClass保留S-PLUS的一致性,但目前没有使用。
If the fdef argument is supplied, take this as the definition of the generic, and test whether it is really a generic, with f as the name of the generic. (This argument is not available in S-Plus.)
如果fdef参数提供,作为通用的定义,并测试它是否真的是一个通用的f,作为通用名称。 (这种说法是不是在S-PLUS)。
If where supplied, just remove the version on this element of the search list; otherwise, removes the first version encountered.
如果where提供的,只是删除此版本的搜索列表中的元素,否则,遇到的第一个版本中删除。
Generic functions should usually have a call to standardGeneric as their entire body. They can, however, do any other computations as well.
通用功能通常应该有一个调用standardGeneric作为自己的整个身体。然而,他们可以做任何其他计算。
The usual setGeneric (directly or through calling setMethod) creates a function with a call to standardGeneric.
通常setGeneric(直接或通过呼吁setMethod)创建一个函数调用一个standardGeneric。
The resulting source file will recreate the method.
由此产生的源文件将重现的方法。
If generic is FALSE, ignore generic functions.
如果generic是FALSE,忽略通用的功能。
If signature is supplied only the methods matching this initial signature are dumped. (This feature is not found in S-Plus: don't use it if you want compatibility.)
如果signature提供只有这个初始签名匹配的方法倾倒。 (此功能未发现在S-PLUS:如果你想的兼容性,不要使用它。)
The advantage of using signature is to provide a check on which arguments you meant, as well as clearer documentation in your method specification. In addition, signature checks that each of the elements is a single character string.
优势使用signature了是提供哪些参数,你的意思是支票,以及更加清晰,方法规范文件。此外,signature检查每个元素是一个字符串。
Returns TRUE if f was a generic function, FALSE (silently) otherwise.
返回TRUE如果f是一个通用的功能,FALSE(默默),否则。
If there is a default method, the function will be re-assigned as a simple function with this definition. Otherwise, the generic function remains but with no methods (so any call to it will generate an error). In either case, a following call to setMethod will consistently re-establish the same generic function as before.
如果有一个默认的方法,该函数将被重新分配这个定义的一个简单的功能。否则,通用的功能仍然没有方法(因此它的任何调用将产生一个错误)。在这两种情况下,其后的一个调用setMethod“将始终如一地重新建立之前相同的通用功能。
参考文献----------References----------
Software for Data Analysis: Programming with R Springer. (For the R version.)
Programming with Data Springer (For the original S4 version.)
参见----------See Also----------
getMethod (also for selectMethod), setGeneric, setClass, showMethods
getMethod(selectMethod)setGeneric,setClass,showMethods
举例----------Examples----------
require(stats) # for lm[为LM]
## get the function "myFun" -- throw an error if 0 or > 1 versions visible:[#得到功能“myFun”的 - 抛出一个错误,如0或1的版本,可见:]
findFuncStrict <- function(fName) {
allF <- findFunction(fName)
if(length(allF) == 0)
stop("No versions of ",fName," visible")
else if(length(allF) > 1)
stop(fName," is ambiguous: ", length(allF), " versions")
else
get(fName, allF[[1]])
}
try(findFuncStrict("myFun"))# Error: no version[错误:没有版本]
lm <- function(x) x+1
try(findFuncStrict("lm"))# Error: 2 versions[错误:2个版本]
findFuncStrict("findFuncStrict")# just 1 version[短短1个版本]
rm(lm)
## method dumping ------------------------------------[#方法倾销------------------------------------]
setClass("A", representation(a="numeric"))
setMethod("plot", "A", function(x,y,...){ cat("A meth\n") })
dumpMethod("plot","A", file="")
## Not run: [#无法运行:]
setMethod("plot", "A",
function (x, y, ...)
{
cat("AAAAA\n")
}
)
## End(Not run)[#结束(不运行)]
tmp <- tempfile()
dumpMethod("plot","A", file=tmp)
## now remove, and see if we can parse the dump[#现在删除,看看我们是否可以解析转储]
stopifnot(removeMethod("plot", "A"))
source(tmp)
stopifnot(is(getMethod("plot", "A"), "MethodDefinition"))
## same with dumpMethods() :[#同dumpMethods():]
setClass("B", contains="A")
setMethod("plot", "B", function(x,y,...){ cat("B ...\n") })
dumpMethods("plot", file=tmp)
stopifnot(removeMethod("plot", "A"),
removeMethod("plot", "B"))
source(tmp)
stopifnot(is(getMethod("plot", "A"), "MethodDefinition"),
is(getMethod("plot", "B"), "MethodDefinition"))
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|