S3Part(methods)
S3Part()所属R语言包:methods
S3-style Objects and S4-class Objects
S3风格的对象和S4级对象
译者:生物统计家园网 机器人LoveR
描述----------Description----------
Old-style (S3) classes may be registered as S4 classes (by calling setOldClass, and many have been. These classes can then be contained in (that is, superclasses of) regular S4 classes, allowing formal methods and slots to be added to the S3 behavior. The function S3Part extracts or replaces the S3 part of such an object. S3Class extracts or replaces the S3-style class. S3Class also applies to object from an S4 class with S3methods=TRUE in the call to setClass.
旧式(三)班的可登记为中S4中类(setOldClass,许多已经通过调用这些类可以包含在(即)定期S4类的超类,允许正式的方法和槽被添加到了S3的行为。功能S3Part提取或替换这样一个对象S3的一部分。S3Class提取或替换S3的样式类。S3Class也适用对象从S4类S3methods=TRUE呼叫setClass。
See the details below. Also discussed are S3 <-> S4 coercion; see the section “S3 and S4 objects”
请参阅下面的详细信息。还讨论了S3 < - >中S4中胁迫;见一节“S3和S4对象”
用法----------Usage----------
S3Part(object, strictS3 = FALSE, S3Class)
S3Part(object, strictS3 = FALSE, needClass = ) <- value
S3Class(object)
S3Class(object) <- value
isXS3Class(classDef)
slotsFromS3(object)
参数----------Arguments----------
参数:object
An object from some class that extends a registered S3 class, usually because the class has as one of its superclasses an S3 class registered by a call to setOldClass, or from a class that extends a basic vector, matrix or array object type. See the details. For most of the functions, an S3 object can also be supplied, with the interpretation that it is its own S3 part.
一些类,扩展了注册的S3类,通常是因为类都有其超一个S3类调用注册的对象setOldClass,或从一个类,扩展了基本的向量,矩阵或数组对象类型。查看详细信息。对于大多数的功能,还可以提供一个S3对象,解释,这是其自己的S3的一部分。
参数:strictS3
If TRUE, the value returned by S3Part will be an S3 object, with all the S4 slots removed. Otherwise, an S4 object will always be returned; for example, from the S4 class created by setOldClass as a proxy for an S3 class, rather than the underlying S3 object.
如果TRUE,S3Part返回值将是一个S3对象,所有删除的S4插槽。否则,将永远被退回S4对象,例如,从创建的S4类setOldClassS3类,而不是底层S3的对象的代理。
参数:S3Class
The character vector to be stored as the S3 class slot in the object. Usually, and by default, retains the slot from object.
特征向量存储S3类对象中的槽。通常情况下,默认情况下,保留了从object插槽。
参数:needClass
Require that the replacement value be this class or a subclass of it.
要求更换价值是这个类或它的一个子类。
参数:value
For S3Part<-, the replacement value for the S3 part of the object. This does not need to be an S4 object; in fact, the usual way to create objects from these classes is by giving an S3 object of the right class as an argument to new. For S3Class<-, the character vector that will be used as a proxy for class(x) in S3 method dispatch. This replacement function can be used to control S3 per-object method selection.
S3Part<-,为对象的S3部分替代的价值。这并不需要是一个中四的对象,事实上,从这些类创建对象的常用方法是给S3对象的正确的类作为new的参数。 S3Class<-,特征向量,将作为代理class(x)S3方法调度使用。这个替换功能可以使用控制S3每个对象方法的选择。
参数:classDef
A class definition object, as returned by getClass.
类定义对象,返回getClass。
Details
详情----------Details----------
Classes that register S3 classes by a call to setOldClass have slot ".S3Class" to hold the corresponding S3 vector of class strings. The prototype of such a class has the value for this slot determined by the argument to setOldClass. Other S4 classes will have the same slot if the argument S3methods = TRUE is supplied to setClass; in this case, the slot is set to the S4 inheritance of the class.
班登记S3通过调用类的setOldClass插槽".S3Class"举行相应的S3类字符串的向量。这样一类的原型为参数setOldClass决定该插槽的价值。如果提供的参数S3methods = TRUEsetClass在这种情况下,插槽设置的S4继承类的其他S4类将具有相同的插槽。
New S4 classes that extend (contain) such classes also have the same slot, and by default the prototype has the value determined by the contains= argument to setClass. Individual objects from the S4 class may have an S3 class corresponding to the value in the prototype or to an (S3) subclass of that value. See the examples below.
新的S4类扩展(含)等类也有相同的插槽,默认情况下,原型有contains=setClass参数确定的值。从S4类的单个对象可能有一个相应的S3类原型的价值或(三)该值的子类。见下面的例子。
S3Part() with strictS3 = TRUE constructs the underlying S3 object by eliminating all the formally defined slots and turning off the S4 bit of the object. With strictS3 = FALSE the object returned is from the corresponding S4 class. For consistency and generality, S3Part() works also for classes that extend the basic vector, matrix and array classes. Since R is somewhat arbitrary about what it treats as an S3 class ("ts" is, but "matrix" is not), S3Part() tries to return an S3 (that is, non-S4) object whenever the S4 class has a suitable superclass, of either S3 or basic object type.
S3Part()与strictS3 = TRUE构造底层S3消除所有的正式定义插槽和关闭的对象中S4中位对象。与strictS3 = FALSE返回的对象是从相应的S4级。一致性和普遍性,S3Part()作品也为扩展的基本向量,矩阵和数组类的类。由于R是有点武断,它作为一个S3类对待("ts",但"matrix")S3Part()试图返回一个S3对象(即,非S4)每当在S4级有一个合适的超类,S3或基本的对象类型。
One general application that relies on this generality is to use S3Part() to get a superclass object that is guaranteed not to be an S4 object. If you are calling some function that checks for S4 objects, you need to be careful not to end up in a closed loop (fooS4 calls fooS3, which checks for an S4 object and calls fooS4 again, maybe indirectly). Using S3Part() with strictS3 = TRUE is a mechanism to avoid such loops.
一个普通的应用程序上依赖于这种普遍性是使用S3Part()得到一个超类对象,保证不会是S4对象。如果你是调用一些功能检查S4的对象,你要小心,不要在一个封闭的循环结束(fooS4要求fooS3,检查为S4对象,并调用fooS4 再次,可能间接地)。使用S3Part()与strictS3 = TRUE是一个机制,以避免此类循环。
Because the contents of S3 class objects have no definition or guarantee, the computations involving S3 parts do not check for slot validity. Slots are implemented internally in R as attributes, which are copied when present in the S3 part. Grave problems can occur if an S4 class extending an S3 class uses the name of an S3 attribute as the name of an S4 slot, and S3 code sets the attribute to an object from an invalid class according to the S4 definition.
S3类对象的内容,因为没有定义或担保,涉及S3的部分不计算插槽有效性检查。插槽研发实施内部属性,当被复制在S3部分。如果S4级扩大S3类作为一个S4插槽的名称使用的S3属性的名称,和S3代码设置属性从一个无效的类的对象,根据S4的定义,可能发生严重问题。
Frequently, S3Part can and should be avoided by simply coercing objects to the desired class; methods are automatically defined to deal correctly with the slots when as is called to extract or replace superclass objects.
频繁,S3Part可以和应该避免通过简单的胁迫对象所需的类,自动定义的方法,正确处理时as被称为提取或替换父类对象的插槽。
The function slotsFromS3() is a generic function used internally to access the slots associated with the S3 part of the object. Methods for this function are created automatically when setOldClass is called with the S4Class argument. Usually, there is only one S3 slot, containing the S3 class, but the S4Class argument may provide additional slots, in the case that the S3 class has some guaranteed attributes that can be used as formal S4 slots. See the corresponding section in the documentation of setOldClass.
功能slotsFromS3()是一个泛型函数内部使用,访问对象S3的插槽。自动创建时setOldClass与S4Class参数调用这个函数的方法。通常情况下,只有一个S3的插槽,包含S3类,但S4Class参数可能的情况下提供额外的插槽,S3类,有一些可以作为正式的中四插槽用来保证属性。看到setOldClass文件中的相应部分。
值----------Value----------
S3Part: Returns or sets the S3 information (and possibly some S4 slots as well, depending on arguments S3Class and keepSlots). See the discussion of argument strict above. If it is TRUE the value returned is an S3 object.
S3Part:返回或设置S3的信息(也可能是一些中四插槽以及参数S3Class和keepSlots)。看到参数strict上面的讨论。如果是的话TRUE返回的值是一个S3对象。
S3Class: Returns or sets the character vector of S3 class(es) stored in the object, if the class has the corresponding .S3Class slot. Currently, the function defaults to class otherwise.
S3Class:返回或设置对象中存储的S3类(ES)的特征向量,如果类具有相应的.S3Class插槽。目前,该功能默认class否则。
isXS3Class: Returns TRUE or FALSE according to whether the class defined by ClassDef extends S3 classes (specifically, whether it has the slot for holding the S3 class).
isXS3Class:返回TRUE或FALSEClassDef定义的类是否扩展S3类(具体而言,它是否有按住S3类的插槽)。
slotsFromS3: returns a list of the relevant slot classes, or an empty list for any other object.
slotsFromS3:返回有关插槽类的列表,或其他任何对象的空列表。
S3和S4对象:转换机制----------S3 and S4 Objects: Conversion Mechanisms----------
Objects in R have an internal bit that indicates whether or not to treat the object as coming from an S4 class. This bit is tested by isS4 and can be set on or off by asS4. The latter function, however, does no checking or interpretation; you should only use it if you are very certain every detail has been handled correctly.
R对象有一个内部位,表明是否治疗的对象,因为从S4类。 isS4测试此位,可以设置或由asS4。然而,后者的功能,并没有检查或解释;你应该只使用它,如果你是正确处理每一个细节都已经非常肯定。
As a friendlier alternative, methods have been defined for coercing to the virtual classes "S3" and "S4". The expressions as(object, "S3") and as(object, "S4") return S3 and S4 objects, respectively. In addition, they attempt to do conversions in a valid way, and also check validity when coercing to S4.
作为一个友好的替代品,方法已定义的胁迫"S3"和"S4"虚拟教室的。表达式as(object, "S3")和as(object, "S4")返回S3和S4对象,分别。此外,他们还尝试做一个有效的方式的转换,并胁迫至S4时,同时检查的有效性。
The expression as(object, "S3") can be used in two ways. For objects from one of the registered S3 classes, the expression will ensure that the class attribute is the full multi-string S3 class implied by class(object). If the registered class has known attribute/slots, these will also be provided.
可以采用两种方式表达as(object, "S3")。对于从一个注册的S3类的对象,表达将确保这个类的属性是完整的多串S3类class(object)暗示。如果注册类称为属性/插槽,这些也将提供。
Another use of as(object, "S3") is to take an S4 object and turn it into an S3 object with corresponding attributes. This is only meaningful with S4 classes that have a data part. If you want to operate on the object without invoking S4 methods, this conversion is usually the safest way.
另一个使用as(object, "S3")采取S4对象,变成S3对象与相应的属性。这仅仅是中S4中,有部分数据的类的有意义的。如果你想在对象上操作,而不调用S4方法,这种转换通常是最安全的方式。
The expression as(object, "S4") will use the attributes in the object to create an object from the S4 definition of class(object). This is a general mechanism to create partially defined version of S4 objects via S3 computations (not much different from invoking new with corresponding arguments, but usable in this form even if the S4 object has an initialize method with different arguments).
表达as(object, "S4")将使用该对象的属性创建从class(object)的中S4中定义的对象。这是一个普遍的机制来创建VIA S3计算(调用没有太大的不同部分定义的S4对象版本new即使与相应的参数,但在这种形式中使用的S4对象initialize方法具有不同的参数)。
参考文献----------References----------
Software for Data Analysis: Programming with R Springer. (For the R version).
Programming with Data Springer (For the original S4 version.)
参见----------See Also----------
setOldClass
setOldClass
举例----------Examples----------
## 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")
## lm.D9 is as computed in the example for stats::lm[#lm.D9被计算在这个例子统计:: LM的]
y1 = new("ylm", lm.D9, header = "test", eps = .1)
xx = new("xlm", lm.D9, eps =.1)
y2 = new("ylm", xx, header = "test")
stopifnot(inherits(y2, "lm"))
stopifnot(identical(y1, y2))
stopifnot(identical(S3Part(y1, strict = TRUE), lm.D9))
## note the these classes can insert an S3 subclass of "lm" as the S3 part:[#注意这些类可以作为S3的一部分插入“LM”S3的子类:]
myData <- data.frame(time = 1:10, y = (1:10)^.5)
myLm <- lm(cbind(y, y^3) ~ time, myData) # S3 class: c("mlm", "lm")[S3类:C(“传销”,“LM”)]
ym1 = new("ylm", myLm, header = "Example", eps = 0.)
##similar classes to "xlm" and "ylm", but extending S3 class c("mlm", "lm")[#类似的类“XLM”和“YLM”,但延长S3 C类(“传销”,“LM”)]
setClass("xmm", representation(eps = "numeric"), contains = "mlm")
setClass("ymm", representation(header="character"), contains = "xmm")
ym2 <- new("ymm", myLm, header = "Example2", eps = .001)
# but for class "ymm", an S3 part of class "lm" is an error:[但YMM“类”,类“lm”S3部分是一个错误:]
try(new("ymm", lm.D9, header = "Example2", eps = .001))
setClass("dataFrameD", representation(date = "Date"),
contains = "data.frame")
myDD <- new("dataFrameD", myData, date = Sys.Date())
## S3Part() applied to classes with a data part (.Data slot)[#S3Part()应用类的数据部分(数据插槽)]
setClass("NumX", contains="numeric", representation(id="character"))
nn = new("NumX", 1:10, id="test")
stopifnot(identical(1:10, S3Part(nn, strict = TRUE)))
m1 = cbind(group, weight)
setClass("MatX", contains = "matrix", representation(date = "Date"))
mx1 = new("MatX", m1, date = Sys.Date())
stopifnot(identical(m1, S3Part(mx1, strict = TRUE)))
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|