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

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

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

                                        Class Definitions
                                         类定义

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

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

Class definitions are objects that contain the formal definition of a class of R objects, usually referred to as an S4 class, to distinguish them from the informal S3 classes. This document gives an overview of S4 classes; for details of the class representation objects, see help for the class classRepresentation.
类定义的对象包含一类R对象的正式定义,通常是指作为一个S4类,以区分他们从非正式S3类。本文档提供的S4类的概述;类的代表性对象的详细信息,请参阅帮助类的classRepresentation。


元数据信息----------Metadata Information----------

When a class is defined, an object is stored that contains the information about that class.  The object, known as the metadata defining the class, is not stored under the name of the class (to allow programmers to write generating functions of that name), but under a specially constructed name. To examine the class definition, call getClass.  The information in the metadata object includes:
当一个类的定义,对象存储,包含有关这个类的信息。的对象,被称为定义类的元数据,存储在类的名称(允许程序员编写生成该名称的功能),但在一个专门建造的名称。检查类的定义,叫getClass。在元数据对象的信息包括:

The data contained in an object from an S4 class is defined by the slots in the class definition.
从S4类的对象中的数据被定义在类定义中的插槽。

Each slot in an object is a component of the object; like components (that is, elements) of a list, these may be extracted and set, using the function slot() or more often the operator "@".  However, they differ from list components in important ways. First, slots can only be referred to by name, not by position, and there is no partial matching of names as with list elements.
对象中的每个插槽是对象的一个组成部分;像组件的列表(即元素),这些可提取和设置,使用功能slot()或更频繁操作"@"。然而,他们不同于列表组件的重要途径。首先,插槽只能被称为按名称,而不是地位,并没有与列表中的元素的名称部分匹配。

All the objects from a particular class have the same set of slot names; specifically, the slot names that are contained in the class definition.  Each slot in each object always is an object of  the class specified for this slot in the definition of the current class. The word “is” corresponds to the R function of the same name (is), meaning that the class of the object in the slot must be the same as the class specified in the definition, or some class that extends the one in the definition (a subclass).
从一个特定的类的所有对象都具有相同的一组插槽名称;具体地说,在类定义中的插槽名称。在每个对象的每个插槽始终是一个槽,这在当前类的定义中指定的类的对象。 “是”字对应的同名的R函数(is),这意味着,在槽的对象类必须是同一类的定义中指定,或某些类扩展在定义中的一个(子类)。

A special slot name, .Data, stands for the "data part" of the object.  An object from a class with a data part is defined by specifying that the class contains one of the R object types or one of the special pseudo-classes, matrix or array, usually because the definition of the class, or of one of its superclasses, has included the type or pseudo-class in its contains argument.  A second special slot name, .xData, is used to enable inheritance from abnormal types such as "environment" See the section on inheriting from non-S4 classes for details on the representation and for the behavior of S3 methods with objects from these classes.
一个特殊的插槽名称,.Data,身高为对象的“部分数据”。一个对象从一类数据部分是指由指定的类包含一个R对象类型或特殊的伪类之一,matrix或array,通常是因为的定义类的,或者其超,包括在其contains参数的类型或伪类。第二个专用插槽名称,.xData,用于启用继承异常类型,如"environment"代表性的细节,从非S4类继承和S3的方法的行为,请参阅节从这些类的对象。

Some slot names correspond to attributes used in old-style S3 objects and in R objects without an explicit class, for example, the names attribute.  If you define a class for which that attribute will be set, such as a subclass of named vectors, you should include "names" as a slot.  See the definition of class "namedList" for an example.  Using the names() assignment to set such names will generate a warning if there is no names slot and an error if the object in question is not a vector type.  A slot called "names" can be used anywhere, but only if it is assigned as a slot, not via the default names() assignment.
一些插槽名称对应使用旧式的S3对象和R对象没有明确的类的属性,例如,names属性。如果你定义一个类将设置该属性,命名为向量的一个子类,如,你应该包括"names"作为一个插槽。看到定义的类"namedList"一个例子。使用names()分配设置等名称将生成一个警告,如果有是没有名字插槽和一个错误,如果有问题的对象是不是一个向量类型。名为"names"的插槽,可以在任何地方使用,但只有当它作为一个时隙分配,而不是通过默认names()分配。

The definition of a class includes the superclasses —the classes that this class extends.  A class Fancy, say, extends a class Simple if an object from the Fancy class has all the capabilities of the Simple class (and probably some more as well).  In particular, and very usefully, any method defined to work for a Simple object can be applied to a Fancy object as well.
一个类的定义,包括超类的类,这个类扩展。 A类Fancy,也就是说,扩展了类Simple如果从对象Fancy类具有的所有功能Simple类(和可能,以及一些更)。 ,尤其是非常有益的,任何方法定义工作1 Simple对象可以应用到一个Fancy对象以及。

This relationship is expressed equivalently by saying that Simple is a superclass of Fancy, or that Fancy is a subclass of Simple.
这种关系是等价表示说Simple是Fancy超,或Fancy是Simple的子类。

The direct superclasses of a class are those superclasses explicitly defined.   Direct superclasses can be defined in three ways.  Most commonly, the superclasses are listed in the contains= argument in the call to setClass that creates the subclass.   In this case the subclass will contain all the slots of the superclass, and the relation between the class is called simple, as it in fact is. Superclasses can also be defined explicitly by a call to setIs; in this case, the relation requires methods to be specified to go from subclass to superclass.   Thirdly, a class union is a superclass of all the members of the union.  In this case too the relation is simple, but notice that the relation is defined when the superclass is created, not when the subclass is created as with the contains= mechanism.
一个类的直接超类是明确定义的超类。可以直接超类定义的三种方式。最常见的是,父contains=参数中列出的呼叫setClass创建子类。在这种情况下将包含子类的超类的所有插槽,和类之间的关系被称为简单,其实它是。父也可以明确定义一个调用setIs;在这种情况下,有关要求被指定去从子类到父类的方法。第三类工会联盟的所有成员是超。在这种情况下,太多的关系是简单的,但通知,父类被创建时定义的关系,而不是当子类被创建contains=机制。

The definition of a superclass will also potentially contain its own direct superclasses.  These are considered (and shown) as superclasses at distance 2 from the original class; their direct superclasses are at distance 3, and so on.  All these are legitimate superclasses for purposes such as method selection.
一个超类的定义也可能会包含它自己的直接超类。这些被认为是(和)在距离从原来的类的父类;其直接超类是在距离3,依此类推。所有这些都是合法的父方法的选择,如目的。

When superclasses are defined  by including the names of superclasses in the contains= argument to setClass, an object from the class will have all the slots defined for its own class and all the slots defined for all its superclasses as well.
当超类定义contains=参数包括父名setClass,从类的对象都定义自己的类和其所有父类的定义为所有插槽插槽好。

The information about the relation between a class and a particular superclass is encoded as an object of class SClassExtension.  A list of such objects for the superclasses (and sometimes for the subclasses) is included in the metadata object defining the class.  If you need to compute with these objects (for example, to compare the distances), call the function extends with argument fullInfo=TRUE.
一个类和一个特别的超之间的关系有关的信息被编码为一个对象类SClassExtension。这些对象的父(有时子类)的列表包含在元数据中定义的类的对象。如果您需要计算这些对象(例如,比较的距离),调用函数extends参数fullInfo=TRUE。

The objects from a class created by a call to new are defined by the prototype object for the class and by additional arguments in the call to new, which are passed to a method for that class for the function initialize.
从一个类中创建的对象通过调用new定义类原型对象由new,这是传递给方法调用该功能的类的其他参数initialize。

Each class representation object contains a prototype object for the class (although for a virtual class the prototype may be NULL). The prototype object must have values for all the slots of the class. By default, these are the prototypes of the corresponding slot classes.  However, the definition of the class can specify any valid object for any of the slots.
每个类表示对象包含一个类的原型对象(虽然一个虚拟类的原型可能是NULL)。原型对象类的所有插槽必须有值。默认情况下,这些都是相应的槽类的原型。然而,类的定义可以指定任何有效的对象为任何插槽。


虚拟类基础班----------Virtual classes; Basic classes----------

Classes exist for which no actual objects can be created by a call to new, the virtual classes, in fact a very important programming tool.  They are used to group together ordinary classes that want to share some programming behavior, without necessarily restricting how the behavior is implemented. Virtual class definitions may if you want include slots (to provide some common behavior without fully defining the object—see the class traceable for an example).
阶级存在,而没有实际的对象可以通过调用一个new,虚拟课堂,其实,一个很重要的编程工具创建。它们被用来组合到一起普通班,想分享一些编程行为,没有一定限制的行为如何实施。虚拟的类定义可能,如果你想包括插槽(提供一些共同的行为,不完全定义的对象类traceable一个例子)。

A simple and useful form of virtual class is the class union, a virtual class that is defined in a call to setClassUnion by listing one or more of subclasses (classes that extend the class union).  Class unions can include as subclasses basic object types (whose definition is otherwise sealed).
一个虚拟类的简单和有用的形式的阶级联盟,虚拟类的定义是在一个由上市调用setClassUnion一个或更多的子类(类,扩展类工会)。级工会可以包括子类对象的基本类型(其定义,否则密封)。

There are a number of "basic" classes, corresponding to the ordinary kinds of data occurring in R.  For example, "numeric" is a class corresponding to numeric vectors. The other vector basic classes are "logical", "integer", "complex", "character",  "raw", "list" and "expression". The prototypes for the vector classes are vectors of length 0 of the corresponding type.  Notice that basic classes are unusual in that the prototype object is from the class itself.
还有的“基本”类的数量,相当于普通的各种数据,例如发生在R,"numeric"是一个类对应的数字向量。其他基本向量类是"logical""integer","complex","character","raw","list"和"expression"。向量类的原型是相应类型的长度为0的向量。请注意,基本类原型对象是从类本身是不寻常的。

In addition to the vector classes there are also basic classes corresponding to objects in the language, such as "function" and "call". These classes are subclasses of the virtual class "language". Finally, there are object types and corresponding basic classes for “abnormal” objects, such as "environment" and "externalptr". These objects do not follow the functional behavior of the language; in particular, they are not copied and so cannot have attributes or slots defined locally.
此外矢量类也有相应的语言对象的基本类,如"function"和"call"。这些类是虚拟类"language"的子类。最后,有“异常”对象,如"environment"和"externalptr",对象类型和相应的基本类。不遵守这些对象的语言功能的行为,尤其是,他们是不可复制的,所以不能有本地定义的属性或插槽。

All these classes can be used as slots or as superclasses for any other class definitions, although they do not themselves come with an explicit class.  For the abnormal object types, a special mechanism is used to enable inheritance as described below.
所有这些类可以用来作为插槽或为任何其他类定义父,虽然他们本身没有拿出一个明确的类。对于异常的对象类型,使用一种特殊的机制,使继承,如下所述。


从非S4继承类----------Inheriting from non-S4 Classes----------

A class definition can extend classes other than regular S4 classes, usually by specifying them in the contains= argument to setClass.  Three groups of such classes behave distinctly:
A类定义可以扩展其他班比普通的中S4中类,通常是由他们指定contains=setClass参数。三组这样的类的行为明显:

S3 classes, which must have been registered by a previous call to setOldClass (you can check that this has been done by calling getClass, which should return a class that extends oldClass);
S3类,必须已经由以前调用注册setOldClass(你可以检查,这已通过调用getClass,它应该返回一个类,扩展oldClass);

One of the R object types, typically a vector type, which then defines the type of the S4 objects, but also a type such as environment that can not be used directly as a type for an S4 object.  See below.
R对象类型之一,通常是一个向量类型,然后定义的S4对象的类型,但也如environment不能直接用作为S4对象的类型。见下文。

One of the pseudo-classes matrix and array, implying objects with arbitrary vector types plus the dim and dimnames attributes.
伪类的matrix和array,这意味着任意的向量类型的对象,加上dim和dimnames属性之一。

This section describes the approach to combining S4 computations with older S3 computations by using such classes as superclasses. The design goal is to allow the S4 class to inherit S3 methods and default computations in as consistent a form as possible.
本节介绍的方法结合使用这些类作为父类的中S4中与旧的S3的计算计算。设计目标是允许的S4类继承S3的方法尽可能一致的形式默认计算。

As part of a general effort to make the S4 and S3 code in R more consistent, when objects from an S4 class are used as the first argument to a non-default S3 method, either for an S3 generic function (one that calls UseMethod) or for one of the primitive functions that dispatches S3 methods, an effort is made to provide a valid object for that method.  In particular, if the S4 class extends an S3 class or matrix or array, and there is an S3 method matching one of these classes, the S4 object will be coerced to a valid S3 object, to the extent that is possible given that there is no formal definition of an S3 class.
作为一个总的努力的一部分,使在R S4和S3的代码更加一致,从S4类的对象时,作为第一个参数使用一个非默认的S3的方法,无论是为S3通用功能(调用UseMethod)或调度S3的方法,努力,该方法提供了一个有效的对象的原始功能之一。特别是,如果S4类扩展S3类或matrix或array,是S3匹配这些类的方法,在S4对象将被裹挟到一个有效的S3对象,以程度上是可能的S3类是没有正式的定义。

For example, suppose "myFrame" is an S4 class that includes the S3 class "data.frame" in the contains= argument to setClass.  If an object from this S4 class is passed to a function, say as.matrix, that has an S3 method for "data.frame", the internal code for UseMethod will convert the object to a data frame; in particular, to an S3 object whose class attribute will be the vector corresponding to the S3 class (possibly containing multiple class names). Similarly for an S4 object inheriting from "matrix" or "array", the S4 object will be converted to a valid S3 matrix or array.
例如,假设"myFrame"是中S4中类,包括S3类"data.frame"contains=setClass参数。如果从这个S4类的一个对象被传递给一个函数,说as.matrix,有"data.frame"的S3方法,UseMethod将对象转换到一个数据框的内部代码;特别是,一个S3对象的类的属性将是相应的S3类(可能包含多个类名)的向量。同样为S4"matrix"或"array",S4的对象将被转换成一个有效的S3矩阵或数组继承的对象。

Note that the conversion is not applied when an S4 object is passed to the default S3 method.  Some S3 generics attempt to deal with general objects, including S4 objects.  Also, no transformation is applied to S4 objects that do not correspond to a selected S3 method; in particular, to objects from a class that does not contain either an S3 class or one of the basic types.  See asS4 for the transformation details.
注意转换S4对象时,传递给默认的S3方法不适用。有些S3的仿制药,试图处理一般的对象,包括S4对象。此外,没有转化应用到S4对象不对应到选定的S3方法;特别是从一个类不包含的S3类或基本类型之一的对象。看到asS4改造的细节。

In addition to explicit S3 generic functions, S3 methods are defined for a variety of operators and functions implemented as primitives.  These methods are dispatched by some internal C code that operates partly through the same code as real S3 generic functions and partly via special considerations (for example, both arguments to a binary operator are examined when looking for methods).  The same mechanism for adapting S4 objects to S3 methods has been applied to these computations as well, with a few exceptions such as generating an error if an S4 object that does not extend an appropriate S3 class or type is passed to a binary operator.
在除了明确S3的通用功能外,S3方法被定义为各种不同的运营商和作为原语实现的功能。这些方法都派出由一些内部的C代码部分是通过相同的代码作为真正的S3的通用功能和部分通过特殊考虑(例如,两个二元运算符的参数寻找方法时,检查)。同样的机制为调整S4对象到S3方法已被应用于这些计算,除少数例外,如生成一个错误,如果S4不延长一个适当的S3类或类型的对象传递给一个二元运算符。

The remainder of this section discusses the mechanisms for inheriting from  basic object types. See matrix or array for inhering from the matrix and array pseudo-classes, or from time-series.  For the corresponding details for inheritance from S3 classes, see setOldClass.
本节的其余部分将讨论的机制,从基本的对象类型继承。看到matrix或array从矩阵和数组的伪类,或从时间序列的继承。从S3类继承相应的详细信息,请参阅setOldClass。

An object from a class that directly and simply contains one of the basic object types in R, has implicitly a corresponding .Data slot of that type, allowing computations to extract or replace the data part while leaving other slots unchanged. If the type is one that can accept attributes and is duplicated normally, the inheritance also determines the type of the object; if the class definition has a .Data slot corresponding to a normal type, the class of the slot determines the type of the object (that is, the value of typeof(x)). For such classes,  .Data is a pseudo-slot; that is, extracting or setting it modifies the non-slot data in the object.  The functions getDataPart and setDataPart are a cleaner, but essentially equivalent way to deal with the data part.
从类对象,直接和简单地包含在R的基本对象类型之一,具有隐含相应的.Data该类型的插槽,允许计算提取或更换部分数据,而把其他插槽不变。如果类型是一个可以接受属性和通常被复制,继承也决定了对象的类型,如果类定义有一个.Data相应的正常类型的插槽,槽类决定的类型对象(即,typeof(x)值)。对于这样的类,.Data是一个伪槽,也就是说,提取或修改对象中的非槽数据。职能getDataPart和setDataPart是一个更清洁,但本质上等价的方式来处理数据的一部分。

Extending a basic type this way allows objects to use old-style code for the corresponding type as well as S4 methods.  Any basic type can be used for .Data, but a few types are treated differently because they do not behave like ordinary objects; for example, "NULL", environments, and external pointers. Classes extend these types by having a slot, .xData, itself inherited from an internally defined S4 class.  This slot actually contains an object of the inherited type, to protect computations from the reference semantics of the type. Coercing to the nonstandard object type then requires an actual computation, rather than the "simple" inclusion for other types and classes.  The intent is that programmers will not need to take account of the mechanism, but one implication is that you should not explicitly use the type of an S4 object to detect inheritance from an arbitrary object type.  Use is and similar functions instead.
延伸这样一个基本类型允许使用旧式代码为相应的类型以及S4方法的对象。可以使用任何基本类型.Data,但一些类型区别对待,因为他们没有像普通的对象,例如,"NULL",环境,和外部的指针。类扩展插槽类型,.xData,本身从内部定义的中S4中类继承。此插槽实际上包含继承的类型,保护类型的引用语义的计算对象。胁迫非标准的对象类型,则需要一个实际的计算,而不是"simple"列入其他类型和类。这样做的目的是,程序员不会需要采取的机制帐户,但一个含义是,你不应该明确使用一个S4对象的类型,检测从任意一个对象类型的继承。使用is“类似的功能,而不是。


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

Software for Data Analysis: Programming with R Springer.  (For the R version.)
Programming with Data Springer (For the original S4 version.)
Statistical Models in S. Wadsworth & Brooks/Cole (Appendix A for S3 classes.)
The New S Language. Wadsworth & Brooks/Cole. (Out of print.) (The description of vectors, matrix, array and time-series objects.)

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

Methods for analogous discussion of methods, setClass for details of specifying class definitions, is, as, new, slot
Methods类似的方法讨论,setClass指定类定义的详细信息,is,as,new,slot

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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-23 22:31 , Processed in 0.028486 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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