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

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

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

                                        Create a Class Definition
                                         创建一个类的定义

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

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

Create  a class definition, specifying the representation (the slots) and/or the classes contained in this one (the superclasses), plus other optional details.
创建一个类的定义,指定代表(槽)和/或(父)在这其中所包含的类,再加上其他可选的细节。


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


setClass(Class, representation, prototype, contains=character(),
         validity, access, where, version, sealed, package,
         S3methods = FALSE)



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

参数:Class
character string name for the class.
字符类的字符串名称。


参数:representation
a named list of the slots that the new class should have, the names giving the names of the slots and the corresponding elements being the character string names of  the corresponding classes. Usually a call to the representation function.  Backward compatibility and compatibility with S-Plus allows unnamed elements for superclasses, but the recommended style is to use the contains= argument instead.
一个插槽,应该有新的类,给予插槽的名称和相应元素的相应类的字符串名称的名称命名的列表。一般representation函数调用。向后兼容性和S-PLUS的兼容性允许无名的父元素,但推荐的方式是使用contains=参数,而不是。


参数:prototype
an object providing the default data for the slots in this class.  Usually and preferably the result of a call to prototype.  
在这个类的插槽提供的默认数据的对象。通常,最好的调用prototype的结果。


参数:contains
what classes does this class extend?  (These are called superclasses in some languages.)  When these classes have slots, all their slots will be contained in the new class as well.  
这个类扩展类? (这些被称为父在某些语言。)当这些类有槽,将所有的插槽,以及在新类中。


参数:where
the environment in which to store or remove the definition.  Defaults to the top-level environment of the calling function (the global environment for ordinary computations, and the environment or namespace of a package in the source code for that package).  
所处的环境来存储或删除的定义。默认调用函数(普通计算的全球环境,并在该包的源代码包的命名空间或环境)的顶级环境。


参数:validity
if supplied, should be a validity-checking method for objects from this class (a function that returns TRUE if its argument is a valid object of this class and one or more strings describing the failures otherwise).  See validObject for details.
如果有提供的话,应该是从这个类的对象有效性检查方法(一个函数,返回TRUE如果它的参数是一个类和一个或多个字符串描述的故障,否则有效的对象)。看到validObject详情。


参数:access, version
access and version, included for compatibility with S-Plus, but currently ignored.
访问和版本,包括S-PLUS的兼容性,但目前被忽略。


参数:sealed
if TRUE, the class definition will be sealed, so that another call to setClass will fail on this class name.  
如果TRUE,类定义将被封闭,使另一个调用setClass会失败,这个类的名字。


参数:package
an optional package name for the class.  By default (and usually) the name of the package in which the class definition is assigned.  
可选包类的名称。默认情况下,通常分配中的类定义的包的名称。


参数:S3methods
if TRUE, S3 methods may be written for this class.  S3 generic functions and primitives will dispatch an S3 method defined for this class, given an S4 object from the class or from a subclass of it, provided no S4 method and no more direct S3 method is found.  Writing S3 methods for S4 classes is somewhat deprecated (see Methods), but if you do write them, the class should be created with this argument TRUE, so inheritance will work.  By default, the current implementation takes no special action, so that methods will be dispatched for this class but not for subclasses.  Note that future versions may revoke this and dispatch no S3 methods other than the default unless S3methods is TRUE.  
如果TRUE,S3的方法可以写这个类。 S3的通用功能和原语将派遣一个S3这个类中定义的方法,从类或它的一个子类对象中S4中,没有提供的S4方法,并没有更直接的S3方法被发现。写作中三中S4中类的方法是有点过时(见方法),但如果你写他们,这种说法TRUE,所以继承工作与创建类应。默认情况下,目前的执引号况没有专项行动,这样的方法将这个类,但没有子类调度。请注意,未来的版本可能撤销派遣默认以外没有S3的方法,除非S3methods是TRUE。


基本用途:角子机和继承----------Basic Use: Slots and Inheritance----------

The two essential arguments, other than the class name are representation and contains, defining the explicit slots and the inheritance (superclasses). Together, these arguments define all the information in an object from this class; that is, the names of all the slots and the classes required for each of them.
两个基本参数,比其他类的名称是representation和contains,确定明确的插槽和继承(父类)。在一起,这些参数定义在这个类的一个对象的所有信息,那就是,所有的插槽,并为他们每个人所需的类的名称。

The name of the class determines which methods apply directly to objects from this class.  The inheritance information specifies which methods apply indirectly, through inheritance.  See Methods.
确定哪些方法适用于直接从这个类的对象的类的名称。指定哪些方法适用于间接通过继承,继承信息。看到方法。

The slots in a class definition will be the union of all the slots specified directly by representation and all the slots in all the contained classes. There can only be one slot with a given name; specifically, the direct and inherited slot names must be unique. That does not, however, prevent the same class from being inherited via more than one path.
在类定义中的插槽将成为联盟的所有直接指定representation“中包含的所有类的所有插槽插槽。只能有一个插槽与一个给定的名称,具体,直接的继承和插槽名称必须是唯一的。 ,但是,这并不防止被通过多个路径继承相同的类。

One kind of element in the contains= argument is special, specifying one of the R object types or one of a few other special R types (matrix and array). See the section on inheriting from object types, below.
一个元素在contains=的说法是特殊的,R对象类型指定一个或其他几个的特殊ŕ类型的(matrix和array)。看到下面的部分,从对象类型继承。

Slot name "class" is not allowed in the current implementation but reserved. "Class" is valid, but undesirable, as it cannot be used in new(<cl>, Class = <slot-value>) (because of argument name matching). There are other slot names with a special meaning; these names start with the "." character.  To be safe, you should define all of your own slots with names starting with an alphabetic character.
插槽名称"class"不允许在目前的执引号况,但保留。 "Class"是有效的,但不可取,因为它不能被用于在new(<cl>, Class = <slot-value>)(因为参数名称匹配)。有其他具有特殊意义的插槽名称,这些名称"."字符开始。为了安全起见,你应该确定自己的所有插槽的名称以字母字符开始。


继承对象类型----------Inheriting from Object Types----------

In addition to containing other S4 classes, a class definition can contain either an S3 class (see the next section) or a built-in R pseudo-class&mdash;one of the R object types or one of the special R pseudo-classes "matrix" and "array". A class can contain at most one of the object types, directly or indirectly. When it does, that contained class determines the &ldquo;data part&rdquo; of the class.
除了含有其他的S4类,一类定义可以包含一个S3类(见下一节)或内置在R伪类的R对象类型的一个或一个特别&#341;伪类"matrix"和"array"。 A类可以包含在大部分的对象类型之一,直接或间接。当它含有类确定的“数据部分”之类的。

Objects from the new class try to inherit the built in behavior of the contained type. In the case of normal R data types, including vectors, functions and expressions, the implementation is relatively straightforward. For any object x from the class, typeof(x) will be the contained basic type; and a special pseudo-slot, .Data, will be shown with the corresponding class. See the "numWithId" example below.
新类的对象,试图继承内置中所包含的类型的行为。在正常的R数据类型,包括向量,函数和表达式的情况下,实现相对简单。任何对象x类typeof(x)将是所包含的基本类型;一个特殊的伪槽,.Data,将显示相应的类。 "numWithId"例如下面。

For an object from any class that does not contain such a type, typeof(x) will be "S4".
对于任何不包含这种类型的类的对象,typeof(x)会"S4"。

Some R data types do not behave normally, in the sense that they are non-local references or other objects that are not duplicated. Examples include those corresponding to classes "environment", "externalptr", and "name". These can not be the types for objects with user-defined classes (either S4 or S3) because setting an attribute overwrites the object in all contexts. It is possible to define a class that inherits from such types, through an indirect mechanism that stores the inherited object in a reserved slot. The implementation tries to make such classes behave as if the object had a data part of the corresponding object type. Methods defined with the object type in the signature should work as should core code that coerces an object to the type in an internal or primitive calculation. There is no guarantee, however, because C-level code may switch directly on the object type, which in this case will be "S4". The cautious mechanism is to use as(x, "environment") or something similar before doing the low-level computation.  See the example for class "stampedEnv" below.
一些数据类型不正常的行为,在某种意义上说,他们都是非本地引用或其他对象不重复。例子包括那些对应的类"environment","externalptr"和"name"。这些都不能与用户定义的类(无论是S4或S3)的对象的类型,因为在所有上下文中的对象设置属性覆盖。这是定义一个类继承这种类型的,间接的机制,继承的对象存储在一个预留插槽。实施,试图使这些类的行为,如果对象有一个相应的对象类型的数据部分。在签名的对象类型定义的方法应该作为核心代码,胁迫在内部或原始的计算类型的对象。然而,也不能保证,因为C级代码可以直接切换的对象类型,在这种情况下,将"S4"。谨慎的机制是使用as(x, "environment")或类似的东西才做低级别计算。看到"stampedEnv"类下面的例子。

Also, keep in mind that the object passed to the low-level computation will be the underlying object type, without any of the slots defined in the class. To return the full information, you will usually have to define a method that sets the data part.
此外,牢记传递给低级别计算的对象将是底层的对象类型,没有任何在类中定义的插槽。返回完整的信息,你通常会定义一个方法,设置数据的一部分。

Note that, in the current implementation, the interpretation of the ".Data" pseudo-slot includes all of the object types above, as well as the special pseudo-classes "matrix" and "array", which R treats internally as if they were object types (they have no explicit class and is.object returns FALSE for such objects). Some of this implementation is still experimental, so a wise policy is to use standard tools, such as as(object, type), to convert to the underlying data type, rather than the pseudo-slot, when possible.
请注意,在目前的执引号况,解释".Data"伪插槽包括所有上述对象类型,以及特殊的伪类"matrix"和"array",其中R内部处理,如果他们的对象类型(他们有没有明确的阶级和is.object返回FALSE这样的对象)。此执行一些仍处于试验阶段,因此,一个明智的政策是使用标准工具,如as(object, type),转换为底层数据类型,而不是伪槽,在可能的情况下。


S3类的继承----------Inheriting from S3 Classes----------

Old-style S3 classes have no formal definition.  Objects are &ldquo;from&rdquo; the class when their class attribute contains the character string considered to be the class name.
旧式的S3类有没有正式的定义。对象是“从”上课的时候,他们的阶级属性包含字符串被认为是类名。

Using such classes with formal classes and methods is necessarily a risky business, since there are no guarantees about the content of the objects or about consistency of inherited methods. Given that, it is still possible to define a class that inherits from an S3 class, providing that class has been registered as an old class (see setOldClass). The essential result is that S3 method dispatch will use the S3 class as registered when dispatching.
使用正规的类和方法等类的,必然是一个高风险业务,因为有对象的内容或有关继承方法的一致性不能保证。鉴于此,仍然有可能定义一个类,它从一个S3类继承,提供已作为一个老类这个类注册(见setOldClass)。重要的结果是,S3方法调度将使用S3类注册时调度。

Some additional options are planned, to control whether the object is converted to an S3 class before dispatch.  In the present implementation, it is not, which causes some S3 computations to misbehave, since they are not seeing the S3 class explicitly.
一些额外的选项,计划,控制对象是否被转换之前,派遣一个S3类。在目前的执引号况,实在不行,这会导致一些S3的计算胡作非为,因为他们没有看到明确的S3类。


类和包----------Classes and Packages----------

Class definitions normally belong to packages (but can be defined in the  global environment as well, by evaluating the expression on the command line or in a file sourced from the command line). The corresponding package name is part of the class definition; that is, part of the classRepresentation object holding that definition.  Thus, two classes with the same name can exist in different packages, for most purposes.
类定义通常属于包(但可以定义以及在全球环境评估在命令行上的表达,或来自命令行的文件,)。相应的包名是类定义的一部分,即classRepresentation定义的对象的一部分。因此,两个同名的类可以存在于不同的包,对于大多数用途。

When a class name is supplied for a slot or a superclass, a corresponding class definition will be found, looking from the namespace or environment of the current package, assuming the call to setClass in question appears directly in the source for the package.  That's where it should appear, to avoid ambiguity.
当一个类的名字为一个槽或超提供一个对应的类定义将被发现,从当前包的命名空间或环境,假设呼叫setClass直接出现在源问题包。这是它应该会出现,以避免歧义。

In particular, if the current package has a namespace then the  class must be found in the current package itself, in the imports defined by that namespace, or in the base package.
特别是,如果当前包有一个命名空间,那么这个类必须在当前包本身,在该命名空间中定义的进口,或在基础包。

When this rule does not identify a class uniquely (because it appears in more than one imported package) then the packageSlot of the character string name needs to be supplied with the name. This should be a rare occurrence.
当这条规则不识别一类独特的(因为它出现在一个以上的进口包),然后packageSlot字符串名称需要提供的名称。这应该是一种罕见的发生。


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

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

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

Classes for a general discussion of classes, Methods for an analogous discussion of methods, makeClassRepresentation
Classes一类的一般性讨论,Methods类似的方法讨论,makeClassRepresentation


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



## A simple class with two slots[#简单的类具有两个插槽]
setClass("track",
         representation(x="numeric", y="numeric"))
## A class extending the previous, adding one more slot[#A类扩大以前,多增加一个插槽]
setClass("trackCurve",
    representation(smooth = "numeric"),
    contains = "track")
## 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 = list(x=numeric(), y=matrix(0,0,0),
                          smooth= matrix(0,0,0)))
##[#]
## Suppose we want trackMultiCurve to be like trackCurve when there's[#假设我们希望trackMultiCurve是像trackCurve的时候有]
## only one column.[#只有一列。]
## First, the wrong way.[#首先,走错了路。]
try(setIs("trackMultiCurve", "trackCurve",
    test = function(obj) {ncol(slot(obj, "y")) == 1}))

## Why didn't that work?  You can only override the slots "x", "y",[#为什么不是工作吗?你只能覆盖插槽“X”,“Y”,]
## and "smooth" if you provide an explicit coerce function to correct[#和“平稳”,如果你提供了一个明确的要挟功能,纠正]
## any inconsistencies:[#任何不一致之处:]

setIs("trackMultiCurve", "trackCurve",
  test = function(obj) {ncol(slot(obj, "y")) == 1},
  coerce = function(obj) {
     new("trackCurve",
         x = slot(obj, "x"),
         y = as.numeric(slot(obj,"y")),
         smooth = as.numeric(slot(obj, "smooth")))
  })

## A class that extends the built-in data type "numeric"[#一个类,扩展内置数据类型的“数字”]

setClass("numWithId", representation(id = "character"),
         contains = "numeric")

new("numWithId", 1:3, id = "An Example")

## inherit from reference object of type "environment"[#继承从“环境”类型的参考对象]
setClass("stampedEnv", contains = "environment",
      representation(update = "POSIXct"))

e1 <- new("stampedEnv",  update = Sys.time())

setMethod("[[<-", c("stampedEnv", "character", "missing"),
   function(x, i, j, ..., value) {
       ev <- as(x, "environment")
       ev[[i]] &lt;- value  #update the object in the environment[更新的环境中的对象]
       x@update &lt;- Sys.time() # and the update time[和更新时间]
       x})

e1[["noise"]] <- rnorm(10)



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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-23 06:13 , Processed in 0.034660 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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