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

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

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

                                        Is an Object from a Class?
                                         是一个从一个类的对象?

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

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

Functions to test inheritance relationships between an object and a class (is) or between two classes (extends), and to establish such relationships (setIs, an explicit alternative to the contains= argument to setClass).
测试对象和类(is)或两个类之间(extends),建立这种关系(setIs,<X一个明确的替代之间的继承关系的函数>contains=)的说法。


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


is(object, class2)

extends(class1, class2, maybe = TRUE, fullInfo = FALSE)

setIs(class1, class2, test=NULL, coerce=NULL, replace=NULL,
      by = character(), where = topenv(parent.frame()), classDef =,
      extensionObject = NULL, doComplete = TRUE)



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

参数:object
any R object.
任何R对象。


参数:class1, class2
the names of the classes between which is relations are to be examined defined, or (more efficiently) the class definition objects for the classes.
之间is关系是要检查的定义,或更有效地为类类定义对象的类的名称。


参数:maybe, fullInfo
In a call to extends, maybe is the value returned if a relation is conditional. In a call with class2 missing, fullInfo is a flag, which if TRUE causes a list of objects of class classExtension to be returned, rather than just the names of the classes.
调用extends在maybe如果一个关系是有条件的返回值。在调用class2失踪,fullInfo是一个标志,如果TRUE导致类对象的名单classExtension被返回,而不是仅仅的名称类。


参数:coerce, replace
In a call to setIs, functions optionally supplied to coerce the object to class2, and to alter the object so that is(object, class2) is identical to value.  See the details section below.
调用setIs在,功能可选择提供强迫对象class2,要改变的对象,所以,is(object, class2)相同value。见下面的细节部分。


参数:test
In a call to setIs, a conditional relationship is defined by supplying this function.  Conditional relations are discouraged and are not included in selecting methods.  See the details section below.  The remaining arguments are for internal use and/or usually omitted.
调用setIs在,有条件的关系被定义为提供此功能。条件关系气馁,不包括在选择方法。见下面的细节部分。剩下的参数是供内部使用和/或通常省略。


参数:extensionObject
alternative to the test, coerce,     replace, by arguments; an object from class SClassExtension describing the relation.  (Used in internal calls.)
替代test, coerce,     replace, by参数;一个对象类SClassExtension描述的关系。 (用于内部通话。)


参数:doComplete
when TRUE, the class definitions will be augmented with indirect relations as well.  (Used in internal calls.)
当TRUE,类定义将扩充以及与间接的关系。 (用于内部通话。)


参数:by
In a call to setIs, the name of an intermediary class. Coercion will proceed by first coercing to this class and from there to the target class.  (The intermediate coercions have to be valid.)
在通话的setIs,中介类的名称。强制将继续首先胁迫这一类,并从那里向目标类。 (中间强制必须是有效的。)


参数:where
In a call to setIs, where to store the metadata defining the relationship.  Default is the global environment for calls from the top level of the session or a source file evaluated there.  When the call occurs in the top level of a file in the source of a package, the default will be the namespace or environment of the package. Other uses are tricky and not usually a good idea, unless you really know what you are doing.
在调用setIs,其中存储的元数据定义的关系。默认是从最高级别的会议或评估有一个源文件调用的全球环境。当呼叫发生在顶层包的源文件,则默认是包的命名空间或环境。其他用途是棘手的,通常不是一个好主意,除非你真的知道你在做什么。


参数:classDef
Optional class definition for class , required internally when setIs is called during the initial definition of the class by a call to setClass. Don't use this argument, unless you really know why you're doing so.
可选的类定义为class当setIs在初始定义的类称为由setClass调用所需的内部。不要使用此参数,除非你真的知道你为什么这样做。


功能汇总----------Summary of Functions----------

With two arguments, tests whether object can be treated as from class2.
有两个参数,测试是否object可以从class2治疗。

With one argument, returns all the super-classes of this object's class.
有一个参数,返回此对象的类的超类。

Does the first class extend the second class? The call returns maybe if the extension includes a test.
没有一流的延长第二类?调用返回maybe如果扩展包括一个测试。

When called with one argument, the value is a vector of the superclasses of class1.  If argument fullInfo  is TRUE, the call returns a named list of objects of class SClassExtension; otherwise, just the names of the superclasses.
当一个参数调用,这个值是一个class1父的向量。如果参数fullInfo是TRUE,调用返回一个列表,否则,只是父的名字命名对象类SClassExtension。

Defines class1 to be an extension (subclass) of class2. If class2 is an existing virtual class, such as a class union, then only the two classes need to be supplied in the call, if the implied inherited methods work for class1. See the details section below.
定义class1扩展class2(子类)。 class2如果是一个现有的虚拟类,如类工会,那么只有两个类需要提供呼叫,如果隐含继承的方法为class1工作。见下面的细节部分。

Alternatively, arguments coerce and replace should be supplied, defining methods to coerce to the superclass and to replace the part corresponding to the superclass. As discussed in the details and other sections below, this form is often less recommended than the corresponding call to setAs, to which it is an alternative.
另外,参数coerce和replace应提供定义方法强迫超类,并更换部分相应的超。在以下的细节和其他部分的讨论,这种形式往往是小于相应的呼叫setAs,它是一种替代的建议。

Argument test allows conditional inheritance, in which the is() result is tested for each object rather than being determined by the class definition.  This form is discouraged when it can be avoided; in particular, note that conditional inheritance is not used to select methods for dispatch.
参数test允许有条件的继承,其中is()结果每个测试对象,而不是类定义确定。这种形式被劝阻时,它可避免的,特别注意,有条件的继承不是用来选择调度方法。


Details

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

Arranging for a class to inherit from another class is a key tool in programming.  In R, there are three basic techniques, the first two providing what is called  &ldquo;simple&rdquo; inheritance, the preferred form:
一类从另一个类继承的安排是在编程的重要工具。在R中,有三个基本技术,前两个提供什么叫做“简单”的继承,首选形式:

By the contains= argument in a call to setClass.  This is and should be the most common mechanism.  It arranges that the new class contains all the structure of the existing class, and in particular all the slots with the same class specified.  The resulting class extension is defined to be simple, with important implications for method definition (see the section on this topic below).
通过contains=调用setClass在参数。这是应该是最常见的机制。安排新的类,包含了所有的现有类结构,特别是同一类的所有插槽中指定。类扩展被定义为simple方法定义的重要意义,(看到下面这个话题的部分)。

Making class1 a subclass of a virtual class either by a call to setClassUnion to make the subclass a member of a new class union, or by a call to setIs to add a class to an existing class union or as a new subclass of an existing virtual class.  In either case, the implication should be that methods defined for the class union or other superclass all work correctly for the subclass.  This may depend on some similarity in the structure of the subclasses or simply indicate that the superclass methods are defined in terms of generic functions that apply to all the subclasses.  These relationships are also generally simple.
class1由setClassUnion使子类的一个新的阶级联盟的成员,或通过调用setIs添加一个类调用子类的一个虚拟类现有一流的工会或作为一个现有的虚拟类的一个新的子类。在任何情况下,其含义应是正确的子类,定义为类的工会或其他超所有工作方法。这可能取决于一些子类结构相似,或者干脆表明,父类的方法是通用的功能,适用于所有的子类中定义。这些关系也普遍简单。

Supplying coerce  and replace arguments to setAs. R allows arbitrary inheritance relationships, using the same mechanism for defining coerce methods by a call to setAs.  The difference between the  two is simply that setAs will require a call to as for a conversion to take place, whereas after the call to setIs, objects will be automatically converted to the superclass.
供应coercereplace和setAs参数。 R允许任意的继承关系,使用定义调用setAs由要挟的方法相同的机制。两者之间的区别仅仅是setAs会as需要转换发生的呼叫,而到setIs呼叫后,对象将被自动转换为超类。

The automatic feature is the dangerous part, mainly because it results in the subclass potentially inheriting methods that do not work.  See the section on inheritance below.  If the two classes involved do not actually inherit a large collection of methods, as in the first example below, the danger may be relatively slight.
自动功能,是危险的部分,主要是因为它导致在子类可能继承方法不起作用。看到继承下文。如果所涉及的两个类实际上没有继承的方法大集合,如下面的第一个例子,危险可能会相对较小。

If the superclass inherits methods where the subclass has only a default or remotely inherited method, problems are more likely. In this case, a general recommendation is to use the setAs mechanism instead, unless there is a strong counter reason. Otherwise, be prepared to override some of  the methods inherited.
如果父类继承的子类的方法只有一个默认或远程继承的方法,问题更容易。在这种情况下,一般建议,而不是使用setAs机制,除非有一个强有力的反理由。否则,准备重写继承的方法。

With this caution given, the rest of this section describes what happens when coerce= and replace= arguments are supplied to setIs.
这个谨慎,本节的其余部分描述发生了什么,当coerce=和到replace=setIs参数提供。

The coerce and replace arguments are functions that define how to coerce a class1 object to class2, and how to replace the part of the subclass object that corresponds to class2.  The first of these is a function of one argument which should be from, and the second of two arguments (from, value).  For details, see the section on coerce functions below .
coerce和replace参数是函数的定义如何强迫一个class1对象class2,以及如何取代对应的子类对象的一部分<X >其中第一个参数是函数应该class2,和第二两个参数(from,from)。有关详细信息,请参阅节以下要挟功能。

When by is specified, the coerce process first coerces to this class and then to class2.  It's unlikely you would use the by argument directly, but it is used in defining cached information about classes.
当by指定,胁迫过程中首先胁迫到这个类,然后class2。这是不太可能,您将直接使用by参数,但它是用来定义类缓存信息。

The value returned (invisibly) by setIs is the revised class definition of class1.
返回的值(不可见)setIs是class1的修订后的类定义。


强迫,更换和测试功能----------Coerce, replace, and test functions----------

The  coerce argument is a function that turns a class1 object into a class2 object.  The replace argument is a function of two arguments that modifies a class1 object (the first argument) to replace the part of it that corresponds to class2 (supplied as value, the second argument).  It then returns the modified object as the value of the call.  In other words, it acts as a replacement method to implement the expression as(object, class2) <- value.
coerce参数是一个函数变成一个class1对象的class2对象。 replace参数是一个修改class1对象(第一个参数),取代它的一部分,对应class2(value提供的两个参数的函数,第二个参数)。然后返回调用的价值作为修改的对象。换句话说,它作为一个替代的方法来实现表达as(object, class2) <- value。

The easiest way to think of the  coerce and replace functions is by thinking of the case that  class1 contains class2 in the usual sense, by including the slots of the second class.  (To repeat, in this situation you would not call setIs, but the analogy shows what happens when you do.)
想最简单的方法coerce和replace功能是思维的情况下,class1包含class2在通常意义上的,包括第二类插槽。 (要重复,在这种情况下,你不会叫setIs,但这个比喻说明,当你这样做会发生什么。)

The coerce function in this case would just make a class2 object by extracting the corresponding slots from the class1 object. The replace function would replace in the class1 object the slots corresponding to class2, and return the modified object as its value.
在这种情况下,功能coerce只想让class2对象由提取class1对象的相应插槽。 replace功能将取代class1反对class2,返回其值修改的对象对应的插槽。

For additional discussion of these functions, see the documentation of the setAs function.  (Unfortunately, argument def to that function corresponds to argument coerce here.)
这些功能的更多讨论,请参阅setAs函数的文档。 (不幸的是,参数def该函数的对应参数coerce在这里。)

The inheritance relationship can also be conditional, if a function is supplied as the test argument.  This should be a function of one argument that returns TRUE or FALSE according to whether the object supplied satisfies the relation is(object, class2). Conditional relations between classes are discouraged in general because they require a per-object calculation to determine their validity. They cannot be applied as efficiently as ordinary relations and tend to make the code that uses them harder to interpret.  NOTE:  conditional inheritance is not used to dispatch methods.  Methods for conditional superclasses will not be inherited.  Instead, a method for the subclass should be defined that tests the conditional relationship.
继承关系,也可以是有条件的,如果一个函数作为test参数提供。这应该是一个函数,返回一个参数TRUE或FALSE根据提供的对象是否满足关系is(object, class2)。有条件类之间的关系气馁一般,因为他们需要每一个对象的计算,以确定其有效性。他们不能作为有效作为普通的关系,往往使代码使用它们更难解释。注:有条件的继承不使用派遣方法。有条件的父类的方法不会被继承。相反,应定义一个子类的方法,测试条件的关系。


继承的方法----------Inherited methods----------

A method written for a particular signature (classes matched to one or more formal arguments to the function) naturally assumes that the objects corresponding to the arguments can be treated as coming from the corresponding classes.  The objects will have all the slots and available methods for the classes.
一个特定的签名写一个方法(类匹配一个或多个函数的形式参数)自然承担相应的参数的对象,可以作为相应的类来处理。对象类和所有插槽可用的方法。

The code that selects and dispatches the methods ensures that this assumption is correct.  If the inheritance was &ldquo;simple&rdquo;, that is, defined by one or more uses of the contains= argument in a call to setClass, no extra work is generally needed.  Classes are inherited from the superclass, with the same definition.
确保这种假设是正确的选择和调度方法的代码。如果继承的是“简单”,就是由一个或多个用途,调用contains=,没有额外的工作,一般需要在setClass参数定义。从超类继承,具有相同的定义。

When inheritance is defined by a general call to setIs, extra computations are required.  This form of inheritance implies that the subclass does not just contain the slots of the superclass, but instead requires the explicit call to the coerce and/or replace method.  To ensure correct computation, the inherited method is supplemented by calls to as before the body of the method is evaluated.
继承时,由一般号召setIs定义,需要额外的计算。这种形式的继承意味着子类并不仅仅包含超插槽,而是需要显式调用的要挟和/或更换方法。为了确保正确的计算,继承的方法所调用as前体的方法进行评估的补充。

The calls to as generated in this case have the argument strict = FALSE, meaning that extra information can be left in the converted object, so long as it has all the appropriate slots.  (It's this option that allows simple subclass objects to be used without any change.)  When you are writing your coerce method, you may want to take advantage of that option.
来电as产生在这种情况下,有论据strict = FALSE,这意味着额外的信息可以在转换后的对象离开,只要有适当的槽。 (这是此选项允许将没有任何变化的情况下使用简单的子类对象。)当你写你的要挟方法,你可能要采取这种选择的优势。

Methods inherited through non-simple extensions can result in ambiguities or unexpected selections.  If class2 is a specialized class with just a few applicable methods, creating the inheritance relation may have little effect on the behavior of class1. But if class2 is a class with many methods, you may find that you now inherit some undesirable methods for class1, in some cases, fail to inherit expected methods. In the second example below, the non-simple inheritance from class "factor" might be assumed to inherit S3 methods via that class.  But the S3 class is ambiguous, and in fact is "character" rather than "factor".
通过简单的扩展继承的方法,可以导致含糊不清或意外的选择。如果class2是一个短短的适用方法的专门类,创建继承关系,可能上class1的行为影响不大。但如果class2是一类有许多方法,你会发现,你现在继承了一些不良的方法class1,在某些情况下,无法继承预期的方法。在下面的第二个例子,非从"factor"类的简单继承可能认为S3的方法,通过这个类继承。但S3类是模糊的,而实际上是"character"而不是"factor"。

For some generic functions, methods inherited by non-simple extensions are either known to be invalid or sufficiently likely to be so that the generic function has been defined to exclude such inheritance.  For example initialize methods must return an object of the target class; this is straightforward if the extension is simple, because no change is made to the argument object, but is essentially impossible.  For this reason, the generic function insists on only simple extensions for inheritance.  See the simpleInheritanceOnly argument to setGeneric for the mechanism.  You can use this mechanism when defining new generic functions.
对于一些通用的功能,非简单的扩展继承的方法是无效的或足够的可能是如此,通用的功能已被定义为排除这种继承。例如initialize方法必须返回一个目标类的对象,这很简单,如果扩展很简单,因为是没有改变的参数对象,但基本上是不可能的。出于这个原因,通用功能,坚持继承,只有简单的扩展。请参阅simpleInheritanceOnly参数到setGeneric机制。定义新的通用功能时,您可以使用此机制。

If you get into problems with functions that do allow non-simple inheritance, there are two basic choices.  Either back off from the setIs call and settle for explicit coercing defined by a call to setAs; or, define explicit methods involving class1 to override the bad inherited methods.  The first choice is the safer, when there are serious problems.
如果你得到的功能,允许非简单的继承问题,有两个基本的选择。要么从setIs调用和调用一个定义明确的胁迫定居setAs;,定义明确的涉及class1覆盖坏继承的方法,为的方法。第一个选择是更安全,当有严重的问题。


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

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

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

inherits is nearly always equivalent to is, both for S4 and non-S4 objects, and is somewhat faster. The non-equivalence applies to classes that have conditional superclasses, with a non-trivial test= in the relation (not common and discouraged):  for these, is tests for the relation but inherits by definition ignores conditional inheritance for S4 objects.
inherits几乎总是相当于is,S4和非S4对象,稍快。非等价的,适用于类有条件父,一个不平凡的test=测试的关系,但is inherits的关系(不常见的和气馁):这些由定义忽略有条件继承S4的对象。

selectSuperClasses(cl) has similar semantics as extends(cl), typically returning subsets of the latter.
selectSuperClasses(cl)extends(cl)类似的语义,通常返回后者的子集。


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



## Two examples of setIs() with coerce= and replace= arguments[#两个例子setIs(强制)=取代=参数]
## The first one works fairly well, because neither class has many[#的第一个作品相当不错,因为这两个类有许多]
## inherited methods do be disturbed by the new inheritance[#继承的方法不被干扰的新继承]

## The second example does NOT work well, because the new superclass,[#第二个例子不工作,因为新超,]
## "factor", causes methods to be inherited that should not be.[“因子”,导致不应该继承的方法。]

## First example:[#第一个例子:]
## a class definition (see \link{setClass} for class "track")[#定义一个类(见\链接“轨道”类{setClass})]
setClass("trackCurve", contains = "track",
         representation( smooth = "numeric"))
## A class similar to "trackCurve", but with different structure[#A类类似为“trackCurve”,但与不同的结构]
## allowing matrices for the "y" and "smooth" slots[#允许矩阵的“Y”和“平稳”插槽]
setClass("trackMultiCurve",
         representation(x="numeric", y="matrix", smooth="matrix"),
         prototype = structure(list(), x=numeric(), y=matrix(0,0,0),

                               smooth= matrix(0,0,0)))
## Automatically convert an object from class "trackCurve" into[#自动转换对象从类“trackCurve”]
## "trackMultiCurve", by making the y, smooth slots into 1-column matrices[#“trackMultiCurve的”,制作成1列矩阵Y,顺利插槽]
setIs("trackCurve",
      "trackMultiCurve",
      coerce = function(obj) {
        new("trackMultiCurve",
            x = obj@x,
            y = as.matrix(obj@y),
            smooth = as.matrix(obj@smooth))
      },
      replace = function(obj, value) {
        obj@y <- as.matrix(value@y)
        obj@x <- value@x
        obj@smooth <- as.matrix(value@smooth)
        obj})




## Second Example:[#第二个例子:]
## A class that adds a slot to "character"[#A类槽,增加了一个“字”]
setClass("stringsDated", contains = "character",
         representation(stamp="POSIXt"))

## Convert automatically to a factor by explicit coerce[#自动转换的一个因素,通过明确的要挟]
setIs("stringsDated", "factor",
      coerce = function(from) factor(from@.Data),
      replace= function(from, value) {
                  from@.Data <- as.character(value); from })

ll <- sample(letters, 10, replace = TRUE)
ld <- new("stringsDated", ll, stamp = Sys.time())

levels(as(ld, "factor"))
levels(ld) # will be NULL--see comment in section on inheritance above.[将NULL  - 在继承以上部分评论。]

## In contrast, a class that simply extends "factor"[#相比之下,一类的简单延伸“因素”]
## has no such ambiguities[#有没有这样的含糊之处]
setClass("factorDated", contains = "factor",
         representation(stamp="POSIXt"))
fd <- new("factorDated", factor(ll), stamp = Sys.time())
identical(levels(fd), levels(as(fd, "factor")))

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


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

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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