setOldClass(methods)
setOldClass()所属R语言包:methods
Register Old-Style (S3) Classes and Inheritance
注册旧式(三)类和继承
译者:生物统计家园网 机器人LoveR
描述----------Description----------
Register an old-style (a.k.a. "S3") class as a formally defined class. The Classes argument is the character vector used as the class attribute; in particular, if there is more than one string, old-style class inheritance is mimicked. Registering via setOldClass allows S3 classes to appear in method signatures, as a slot in an S4 class, or as a superclass of an S4 class.
注册作为一个正式的定义类的旧式(又名“S3的)类。 Classes参数是class属性的特征向量;,特别是如果有一个以上的字符串,旧式类继承模仿。注册通过setOldClass允许S3类出现在方法签名,作为一个在S4级的插槽,或作为一个S4级的超。
用法----------Usage----------
setOldClass(Classes, prototype, where, test = FALSE, S4Class)
参数----------Arguments----------
参数:Classes
A character vector, giving the names for S3 classes, as they would appear on the right side of an assignment of the class attribute in S3 computations. In addition to S3 classes, an object type or other valid data part can be specified, if the S3 class is known to require its data to be of that form.
一个特征向量,S3类的名称,因为它们将出现在右侧class属性在S3计算分配。此外S3类,对象的类型或其他有效数据部分可以指定,如果S3类被称为要求其数据是这种形式。
参数:prototype
An optional object to use as the prototype. This should be provided as the default S3 object for the class. If omitted, the S4 class created to register the S3 class is VIRTUAL. See the details.
使用一个可选的对象为原型。这应作为默认的S3的类的对象。如果省略,注册S3类创建的S4级VIRTUAL。查看详细信息。
参数:where
Where to store the class definitions, the global or top-level environment by default. (When either function is called in the source for a package, the class definitions will be included in the package's environment by default.)
存储类定义,默认情况下,全球顶级的环境。 (当两个函数是在一个包的源,类定义将包括默认情况下,在包的环境。)
参数:test
flag, if TRUE, arrange to test inheritance explicitly for each object, needed if the S3 class can have a different set of class strings, with the same first string. This is a different mechanism in implementation and should be specified separately for each pair of classes that have an optional inheritance. See the "Details".
标志,如果TRUE,安排测试的每个对象,如果S3类可以有不同类的字符串,用相同的第一个字符串,需要继承明确。这是一个不同的机制和实施,应分别指定对每一类有一个可选的继承。看到“详细资料”。
参数:S4Class
optionally, the class definition or the class name of an S4 class. The new class will have all the slots and other properties of this class, plus its S3 inheritance as defined by the Classses argument. Arguments prototype and test must not be supplied in this case. See the section on “S3 classes with known attributes” below.
可选的类定义或一个S4类的类名。新的类,将所有的插槽,这个类的其他属性,再加上其S3继承Classses参数定义。参数prototype和test在这种情况下,不得提供。看到“S3类与已知的属性”一节。
Details
详情----------Details----------
Each of the names will be defined as an S4 class, extending the remaining classes in Classes, and the class oldClass, which is the "root" of all old-style classes. S3 classes have no formal definition, and therefore no formally defined slots. If a prototype argument is supplied in the call to setOldClass(), objects from the class can be generated, by a call to new; however, this usually not as relevant as generating objects from subclasses (see the section on extending S3 classes below). If a prototype is not provided, the class will be created as a virtual S4 class. The main disadvantage is that the prototype object in an S4 class that uses this class as a slot will have a NULL object in that slot, which can sometimes lead to confusion.
每个名字将被定义为S4类,延长Classes类oldClass,这是“根”的所有旧式类的剩余类。 S3类有没有正式的定义,因此没有正式定义的插槽。如果提供一个prototype参数在调用setOldClass(),从类对象可以生成,调用new;然而,这通常不是作为生成对象的有关从子类(见扩展下面的S3类的部分)。如果没有提供一个原型,类将创建一个虚拟的S4级。主要缺点是,在S4级的原型对象,使用这个类作为一个插槽将有一个NULL对象在该插槽,这有时会导致混乱。
Beginning with version 2.8.0 of R, support is provided for using a (registered) S3 class as a superclass of a new S4 class. See the section on extending S3 classes below, and the examples.
与R的2.8.0版本开始,作为一个新的S4类的超类(注册)S3类提供支持。见部分,扩展下面的S3类的例子。
See Methods for the details of method dispatch and inheritance.
看到调度和继承方法的细节的方法。
Some S3 classes cannot be represented as an ordinary combination of S4 classes and superclasses, because objects from the S3 class can have a variable set of strings in the class. It is still possible to register such classes as S4 classes, but now the inheritance has to be verified for each object, and you must call setOldClass with argument test=TRUE once for each superclass.
有些S3类不能作为一个普通的S4类和父类的组合来表示,因为从S3类的对象可以在类中有一个字符串变量集。登记与S4类等类,它仍然是可能的,但现在的继承必须验证每个对象,你必须调用setOldClass参数test=TRUE一次为每个超。
For example, ordered factors always have the S3 class c("ordered", "factor"). This is proper behavior, and maps simply into two S4 classes, with "ordered" extending "factor".
例如,责令因素总是有S3类c("ordered", "factor")。这是正确的行为,并简单地分为两个S4类的地图,"ordered"延长"factor"。
But objects whose class attribute has "POSIXt" as the first string may have either (or neither) of "POSIXct" or "POSIXlt" as the second string. This behavior can be mapped into S4 classes but now to evaluate is(x, "POSIXlt"), for example, requires checking the S3 class attribute on each object. Supplying the test=TRUE argument to setOldClass causes an explicit test to be included in the class definitions. It's never wrong to have this test, but since it adds significant overhead to methods defined for the inherited classes, you should only supply this argument if it's known that object-specific tests are needed.
但是,对象的类属性"POSIXt"可能有(或无)"POSIXct"或"POSIXlt"第二个字符串作为第一个字符串。这种行为可以被映射到中四班级,但现在评估is(x, "POSIXlt"),例如,需要检查每个对象的S3类的属性。供应test=TRUE参数setOldClass导致包含在类定义一个明确的测试。有这个测试,这是从来没有错,但因为它增加了显着的开销为继承类中定义的方法,你应该只提供这种说法,如果它被称为特定对象的测试需要。
The list .OldClassesList contains the old-style classes that are defined by the methods package. Each element of the list is a character vector, with multiple strings if inheritance is included. Each element of the list was passed to setOldClass when creating the methods package; therefore, these classes can be used in setMethod calls, with the inheritance as implied by the list.
列表.OldClassesList包含旧式的方法包定义的类。列表中的每个元素是一个字符向量,多个字符串,如果包含了继承。列表中的每个元素传递给setOldClassmethods包;因此,这些类可以使用setMethod电话暗示继承列表,创建。
扩展S3的类----------Extending S3 classes----------
A call to setOldClass creates formal classes corresponding to S3 classes, allows these to be used as slots in other classes or in a signature in setMethod, and mimics the S3 inheritance.
一个setOldClass调用创建相应的S3类的正规类,让这些被作为其他类的插槽或使用在setMethod签名,模仿S3的继承。
In documentation for the initial implementation of S4 classes in R, users were warned against defining S4 classes that contained S3 classes, even if those had been registered. The warning was based mainly on two points. 1: The S3 behavior of the objects would fail because the S3 class would not be visible, for example, when S3 methods are dispatched. 2: Because S3 classes have no formal definition, nothing can be asserted in general about the S3 part of an object from such a class. (The warning was repeated as recently as the first reference below.)
S4类研发的初步实施的文件中,用户对定义包含S3类的中四班级被警告,即使已登记。主要基于两点警告。 1:S3的行为对象会失败,因为S3类是不可见的,例如,S3的方法时,都派出。 2:由于S3类有没有正式的定义,没有什么可以断言在S3的一部分,从这样一个类的对象一般。 (警告,最近多次被作为第一个参考下面。)
Nevertheless, defining S4 classes to contain an S3 class and extend its behavior is attractive in many applications. The alternative is to be stuck with S3 programming, without the flexibility and security of formal class and method definitions.
然而,确定S4类包含S3级,并延长其行为是在许多应用中的吸引力。另一种方法是被卡住,没有正式的类和方法定义的灵活性和安全性,与S3编程。
Beginning with version 2.8.0, R provides support for extending registered S3 classes; that is, for new classes defined by a call to setClass in which the contains= argument includes an S3 class. See the examples below. The support is aimed primarily at providing the S3 class information for all classes that extend class oldClass, in particular by ensuring that all objects from such classes contain the S3 class in a special slot.
从2.8.0版本开始,R提供延长登记S3的类的支持,也就是说,调用定义新类setClass在contains=参数包括S3类。见下面的例子。主要针对的是提供的所有类,扩展类oldClass,特别是确保从这些类的所有对象都包含在一个特殊的槽S3类的S3类信息的支持。
There are three different ways to indicate an extension to an existing S3 class: setOldClass(), setClass() and setIs(). In most cases, calling setOldClass is the best approach, but the alternatives may be preferred in the special circumstances described below.
有三种不同的方式表明现有的S3类的扩展:setOldClass(),setClass()和setIs()。在大多数情况下,调用setOldClass是最好的办法,但替代品,可在下面描述的特殊情况下的首选。
Suppose "A" is any class extending "oldClass". then
假设"A"是延长"oldClass"任何类。然后
setOldClass(c("B", "A"))
setOldClass(c("B", "A"))
creates a new class "B" whose S3 class concatenates "B" with S3Class("A"). The new class is a virtual class. If "A" was defined with known attribute/slots, then "B" has these slots also; therefore, you must believe that the corresponding S3 objects from class "B" do indeed have the claimed attributes. Notice that you can supply an S4 definition for the new class to specify additional attributes (as described in the next section.) The first alternative call produces a non-virtual class.
创建一个新的类"B"的S3"B"与S3Class("A")类串连。新的类是一个虚拟类。如果"A"定义与已知的属性/插槽,然后"B"这些插槽,因此,你必须相信相应的S3类对象"B"的确有声称属性。请注意,您可以提供为中S4中定义新的类来指定额外的属性(如下一节所述。)第一替代调用产生一个非虚类。
setClass("B", contains = "A")
setClass("B", contains = "A")
This creates a non-virtual class with the same slots and superclasses as class "A". However, class "B" is not included in the S3 class slot of the new class, unless you provide it explicitly in the prototype.
这将创建一个具有相同的插槽和父类"A"非虚拟类。然而,类"B"不包括在S3类的一类新的插槽,除非你提供的原型,它明确。
setClass("B"); setIs("B", "A", .....)
setClass("B"); setIs("B", "A", .....)
This creates a virtual class that extends "A", but does not contain the slots of "A". The additional arguments to setIs should provide a coerce and replacement method. In order for the new class to inherit S3 methods, the coerce method must ensure that the class "A" object produced has a suitable S3 class. The only likely reason to prefer this third approach is that class "B" is not consistent with known attributes in class "A".
这将创建一个虚拟类,延伸"A"的,但不包含"A"插槽。 setIs的额外参数应提供要挟和更换方法。在新类继承S3的方法,要挟的方法必须确保该类"A"对象产生一个合适的S3类。喜欢这第三种方法是唯一可能的原因是,类"B"类"A"属性是不相符。
Beginning with version 2.9.0 of R, objects from a class extending an S3 class will be converted to the corresponding S3 class when being passed to an S3 method defined for that class (that is, for one of the strings in the S3 class attribute). This is intended to ensure, as far as possible, that such methods will work if they work for ordinary S3 objects. See Classes for details.
与R的2.9.0版本开始,从S3类将被转换为相应的S3类(即S3类属性的字符串被传递到一个S3为该类定义的方法时,扩展类的对象)。这样做的目的,尽可能确保,这种方法将工作,如果他们为对象的普通S3的工作。看到Classes详情。
S3类与已知的属性----------S3 Classes with known attributes----------
A further specification of an S3 class can be made if the class is guaranteed to have some attributes of known class (where as with slots, “known” means that the attribute is an object of a specified class, or a subclass of that class).
一个S3类的进一步规范,如果类是保证有一些已知的类的属性(可与插槽,“已知”意味着该属性指定类的对象,这个类的一个子类)。
In this case, the call to setOldClass() can supply an S4 class definition representing the known structure. Since S4 slots are implemented as attributes (largely for just this reason), the know attributes can be specified in the representation of the S4 class. The usual technique will be to create an S4 class with the desired structure, and then supply the class name or definition as the argument S4Class to setOldClass().
在这种情况下,调用setOldClass()可以提供一个已知结构的S4类定义。由于S4插槽属性(主要是因为这个原因)实施后,知道属性可以在指定的S4类的代表性。通常的技术将创建一个具有所需结构的中S4中类,然后提供作为参数S4Class类的名称或定义setOldClass()。
See the definition of class "ts" in the examples below. The call to setClass to create the S4 class can use the same class name, as here, so long as the class definition is not sealed. In the example, we define "ts" as a vector structure with a numeric slot for "tsp". The validity of this definition relies on an assertion that all the S3 code for this class is consistent with that definition; specifically, that all "ts" objects will behave as vector structures and will have a numeric "tsp" attribute. We believe this to be true of all the base code in R, but as always with S3 classes, no guarantee is possible.
看到类"ts"在下面的例子定义。调用setClass创建的S4级可以使用相同的类的名称,在这里,只要在类定义不密封。在这个例子中,我们定义"ts"作为向量与数字插槽"tsp"结构。这个定义的有效性依赖于一个断言,所有S3这个类的代码,这个定义是一致的;具体而言,所有"ts"对象将表现为向量的结构将有一个数字"tsp"属性。我们相信这是真正的研发基地的所有代码,但始终与S3类,不能保证是可能的。
The S4 class definition can have virtual superclasses (as in the "ts" case) if the S3 class is asserted to behave consistently with these (in the example, time-series objects are asserted to be consistent with the structure class).
S4类定义可以有虚拟父(如"ts"情况的),如果S3类行为(例如,时间序列对象声称要与结构类)一贯断言。
For another example, look at the S4 class definition for "data.frame".
另一个例子,看看"data.frame"S4类定义。
Be warned that failures of the S3 class to live up to its asserted behavior will usually go uncorrected, since S3 classes inherently have no definition, and the resulting invalid S4 objects can cause all sorts of grief. Many S3 classes are not candidates for known slots, either because the presence or class of the attributes are not guaranteed (e.g., dimnames in arrays, although these are not even S3 classes), or because the class uses named components of a list rather than attributes (e.g., "lm"). An attribute that is sometimes missing cannot be represented as a slot, not even by pretending that it is present with class "NULL", because attributes unlike slots can not have value NULL.
被警告S3类故障,不辜负其声称的行为通常会去裸,因为S3类本身没有定义,产生的无效的S4对象可以导致各种悲伤。许多类S3是不是知名插槽候选人,要么是因为存在或类的属性,但不保证(例如,dimnames阵列,虽然这些都不是连S3类),或因为类使用名为组件而不是属性列表(例如,"lm")。有时缺少一个属性,不能作为一个槽来表示,甚至没有假装,这是目前带班,"NULL",因为不像插槽的属性不能有值NULL。
One irregularity that is usually tolerated, however, is to optionally add other attributes to those guaranteed to exist (for example, "terms" in "data.frame" objects returned by model.frame). As of version 2.8.0, validity checks by validObject ignore extra attributes; even if this check is tightened in the future, classes extending S3 classes would likely be exempted because extra attributes are so common.
然而,一个不规则的耐受性通常是选择添加其他属性,以保证不存在(例如,"terms""data.frame" model.frame对象返回)。为2.8.0版,有效性检查由validObject忽略额外的属性;此检查,即使是在未来收紧,可能会获得豁免延长S3类的类,因为额外的属性是很常见的。
参考文献----------References----------
Software for Data Analysis: Programming with R Springer. (For the R version: see section 10.6 for method selection and section 13.4 for generic functions).
Programming with Data Springer (For the original S4 version.)
参见----------See Also----------
setClass, setMethod
setClass,setMethod
举例----------Examples----------
require(stats)
setOldClass(c("mlm", "lm"))
setGeneric("dfResidual", function(model)standardGeneric("dfResidual"))
setMethod("dfResidual", "lm", function(model)model$df.residual)
## dfResidual will work on mlm objects as well as lm objects[#dfResidual将传销的对象以及LM对象的工作]
myData <- data.frame(time = 1:10, y = (1:10)^.5)
myLm <- lm(cbind(y, y^3) ~ time, myData)
showClass("data.frame")# to see the predefined S4 "oldClass"[看到预定的S4“oldClass”]
## two examples extending S3 class "lm", class "xlm" directly[#两个例子延伸S3类“LM”,“类”XLM直接]
## and "ylm" indirectly[#和间接“YLM”]
setClass("xlm", representation(eps = "numeric"), contains = "lm")
setClass("ylm", representation(header = "character"), contains = "xlm")
ym1 = new("ylm", myLm, header = "Example", eps = 0.)
## for more examples, see ?\link{S3Class}.[#更多的例子,请参阅\链接{S3Class}的。]
utils::str(.OldClassesList)
## Examples of S3 classes with guaranteed attributes[#S3类的例子有保证的属性]
## an S3 class "stamped" with a vector and a "date" attribute[#S3类矢量和“日期”属性的“盖章”]
## Here is a generator function and an S3 print method.[#这里是一台发电机的功能和S3打印方法。]
## NOTE: it's essential that the generator checks the attribute classes[#注:这是至关重要的发电机检查属性类]
stamped <- function(x, date = Sys.time()) {
if(!inherits(date, "POSIXt"))
stop("bad date argument")
if(!is.vector(x))
stop("x must be a vector")
attr(x, "date") <- date
class(x) <- "stamped"
x
}
print.stamped <- function(x, ...) {
print(as.vector(x))
cat("Date: ", format(attr(x,"date")), "\n")
}
## Now, an S4 class with the same structure:[#现在,S4类具有相同的结构:]
setClass("stamped4", contains = "vector", representation(date = "POSIXt"))
## We can use the S4 class to register "stamped", with its attributes:[#我们可以使用的S4类注册“盖章”,其属性:]
setOldClass("stamped", S4Class = "stamped4")
selectMethod("show", "stamped")
## and then remove "stamped4" to clean up[#,然后取出“stamped4”清理]
removeClass("stamped4")
someLetters <- stamped(sample(letters, 10),
ISOdatetime(2008, 10, 15, 12, 0, 0))
st <- new("stamped", someLetters)
st
# show() method prints the object's class, then calls the S3 print method.[show()方法打印出对象的类,然后调用S3打印方法。]
stopifnot(identical(S3Part(st, TRUE), someLetters))
# creating the S4 object directly from its data part and slots[创建的S4对象直接从它的部分数据和槽]
new("stamped", 1:10, date = ISOdatetime(1976, 5, 5, 15, 10, 0))
## Not run: [#无法运行:]
## The code in R that defines "ts" as an S4 class[#代码在R定义“TS”作为S4类]
setClass("ts", contains = "structure",
representation(tsp = "numeric"),
prototype(NA, tsp = rep(1,3)))
# prototype to be a legal S3 time-series[原型是一个合法的S3的时间序列]
## and now registers it as an S3 class[#现在注册为S3类]
setOldClass("ts", S4Class = "ts", where = envir)
## End(Not run)[#结束(不运行)]
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|