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

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

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

                                        Force an Object to Belong to a Class
                                         强制对象属于一个类

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

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

These functions manage the relations that allow coercing an object to a given class.
这些职能管理的关系,使胁迫一个给定的类的对象。


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


as(object, Class, strict=TRUE, ext)

as(object, Class) <- value

setAs(from, to, def, replace, where = topenv(parent.frame()))




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

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


参数:Class
the name of the class to which object should be coerced.  
的object应该被强迫的类的名称。


参数:strict
logical flag.  If TRUE, the returned object must be strictly from the target class (unless that class is a virtual class, in which case the object will be from the closest actual class, in particular the original object, if that class extends the virtual class directly).  If strict = FALSE, any simple extension of the target class will be returned, without further change.  A simple extension is, roughly, one that just adds slots to an existing class.
逻辑标志。如果TRUE,返回的对象必须是严格的目标类(除非该类别是一个虚拟的类,在这种情况下,该对象将是最接近实际的类,特别是原始对象,如果类扩展直接虚拟类)。如果strict = FALSE,任何目标类的简单延伸,将返回没有进一步的变化。一个简单的扩展,大致是,只是增加了插槽现有类。


参数:value
The value to use to modify object (see the discussion below).  You should supply an object with class Class; some coercion is done, but you're unwise to rely on it.
要使用的值修改object(见下面的讨论)。你应该提供一个类的对象Class;做一些强制的,但你是不明智的,依靠它。


参数:from, to
The classes between which the coerce methods def and replace perform coercion.  
类之间的要挟的方法def和replace执行强制。


参数:def
function of one argument.  It will get an object from class from and had better return an object of class to. The convention is that the name of the argument is from; if another argument name is used, setAs will attempt to substitute from.  
一个参数的功能。它将从类from得到一个对象,并更好地返回一个对象的类to。该公约是参数的名称是from;如果使用另一个参数的名称,setAs会尝试替换from。


参数:replace
if supplied, the function to use as a replacement method, when as is used on the left of an assignment. Should be a function of two arguments, from, value, although setAs will attempt to substitute if the arguments differ.
如果提供的功能使用作为替代方法,当as转让左侧的。应该是两个参数的函数,from, value,但setAs会尝试替换,如果参数不同。


参数:where
the position or environment in which to store the resulting methods. For most applications, it is recommended to omit this argument and to include the call to setAs in source code that is evaluated at the top level; that is, either in an R session by something equivalent to a call to source, or as part of the R source code for a package.
位置或环境,在其中存储所产生的方法。对于大多数应用,建议省略此参数,包括调用setAs源代码中被评为在顶层,即在R会话的东西,相当于调用一个source,r源代码包的一部分。


参数:ext
the optional object defining how Class is extended by the class of the object (as returned by possibleExtends). This argument is used internally (to provide essential information for non-public classes), but you are unlikely to want to use it directly.  
可选的对象定义如何Class延长对象的类(如由possibleExtends返回)。这种说法是内部使用(提供非公共类的基本信息),但你不太想直接使用它。


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

Returns the version of this object coerced to be the given Class.  When used in the replacement form on the left of an assignment, the portion of the object corresponding to Class is replaced by value.
返回的这种裹挟对象的版本,以给定的Class。当用于更换的形式转让左侧,Class相应的对象的一部分被替换value。

The operation of as() in either form depends on the definition of coerce methods.  Methods are defined automatically when the two classes are related by inheritance; that is, when one of the classes is a subclass of the other.  See the section on inheritance  below for details.
as()在任何形式的操作取决于要挟方法的定义。方法时自动定义两个类继承关系,也就是说,当一个类是其他的一个子类。见节上继承,详情如下。

Coerce methods are also predefined for basic classes (including all the types of vectors, functions and a few others). See showMethods(coerce) for a list of these.
要挟的方法也预定义的基本类(包括所有类型的向量,功能和其他一些)。看到showMethods(coerce)这些名单。

Beyond these two sources of methods, further methods are defined by calls to the setAs function.
除了这两个来源的方法,进一步方法定义由setAs函数的调用。

Define methods for coercing an object of class from to be of class to; the def argument provides for direct coercing and the replace argument, if included, provides for replacement.  See the “How” section below for details.
胁迫类对象定义的方法from类to;def参数提供直接胁迫replace参数,如果包括,提供更换。见“如何”下面的细节部分。

Coerce from to be of the same class as to.
要挟from是同一类的to。

These functions should not be called explicitly.  The function setAs creates methods for them for the as function to use.
这些功能不应该被称为明确。的功能setAsas函数使用为他们创建的方法。


继承和胁迫----------Inheritance and Coercion----------

Objects from one class can turn into objects from another class either automatically or by an explicit call to the as function.  Automatic conversion is special, and comes from the designer of one class of objects asserting that this class extends another class.  The most common case is that one or more class names are supplied in the contains= argument to setClass, in which case the new class extends each of the earlier classes (in the usual terminology, the earlier classes are superclasses of the new class and it is a subclass of each of them).
从一个类的对象可以转化为对象,从另一个类自动或通过显式调用as功能。自动转换是特殊的,从一类断言,这个类扩展了另一个类的对象的设计师。最常见的情况是,一个或多个类名contains=参数提供setClass,在这种情况下,新的类扩展的早期类(常用的术语,早期类新类的父,这是他们每个人的一个子类)。

This form of inheritance is called simple inheritance in R. See setClass for details. Inheritance can also be defined explicitly by a call to setIs. The two versions have slightly different implications for coerce methods. Simple inheritance implies that inherited slots behave identically in the subclass and the superclass. Whenever two classes are related by simple inheritance, corresponding coerce methods are defined for both direct and replacement use of as. In the case of simple inheritance, these methods do the obvious computation:  they extract or replace the slots in the object that correspond to those in the superclass definition.
这种形式的继承被称为R中的简单继承,见setClass详情。继承也可以被定义明确调用setIs由。这两个版本有要挟的方法略有不同的影响。简单的继承意味着,在相同的子类和父类继承的插槽行为。每当两个类是由简单的继承关系,相应的要挟方法被定义为直接和更换使用as。在简单的继承的情况下,这些方法做了明显的计算:提取或更换插槽,对应超类定义的对象。

The implicitly defined coerce methods may be overridden by a call to setAs; note, however, that the implicit methods are defined for each subclass-superclass pair, so that you must override each of these explicitly, not rely on inheritance.
隐式定义的要挟方法可能覆盖;调用setAs由注意,但是,隐式方法被定义为每个子超对,所以你必须重写这些明确的,不依靠继承。

When inheritance is defined by a call to setIs, the coerce methods are provided explicitly, not generated automatically. Inheritance will apply (to the from argument, as described in  the section below). You could also supply methods via setAs for non-inherited relationships, and now these also can be inherited.
当继承调用setIs由定义,要挟的方法明确规定,不会自动生成。继承将申请(from的说法,在下面一节中所述)。你也可以通过setAs提供方法非继承关系,而现在这些也可以继承。

For further on the distinction between simple and explicit inheritance, see setIs.
为进一步简单和明确的继承之间的区别,看到setIs。


功能,作为和setAs工作----------How Functions 'as' and 'setAs' Work----------

The function as  turns object into an object of class Class.  In doing so, it applies a &ldquo;coerce method&rdquo;, using S4 classes and methods, but in a somewhat special way. Coerce methods are methods for the function coerce or, in the replacement case the function `coerce<-`. These functions have two arguments in method signatures, from and to, corresponding to the class of the object and the desired coerce class. These functions must not be called directly, but are used to store tables of methods for the use of as, directly and for replacements. In this section we will describe the direct case, but except where noted the replacement case works the same way, using `coerce<-` and the replace argument to setAs, rather than coerce and the def argument.
功能as变成object类Class对象到。这样做,它适用于“强制法”,使用中S4中的类和方法,但在有些特殊的方式。要挟的方法是功能coerce或更换的情况下,函数coerce<-的方法。这些函数有两个参数在方法签名,from和to,对应的类的对象和所需的要挟类。不能被直接调用这些功能,但使用存储as,直接使用和替代的方法表。在本节中,我们将介绍直接的情况,但除了注意的地方更换的情况下,以同样的方式,使用coerce<-和replacesetAs参数,而不是coerce def参数。

Assuming the object is not already of the desired class, as first looks for a method in the table of methods for the function coerce for the signature c(from = class(object), to =     Class), in the same way method selection would do its initial lookup. To be precise, this means the table of both direct and inherited methods, but inheritance is used specially in this case (see below).
假设object是不是已经所需的类,ascoerce签名c(from = class(object), to =     Class),在相同的功能的方法在表的方法的第一次时装展示选择的方式方法,将尽其初始的查找。确切地说,这意味着表直接和继承的方法,但在这种情况下,专门用于继承(见下文)。

If no method is found, as looks for one. First, if either Class or class(object) is a superclass of the other, the class definition will contain the information needed to construct a coerce method. In the usual case that the subclass contains the superclass (i.e., has all its slots), the method is constructed either by extracting or replacing the inherited slots. Non-simple extensions (the result of a call to setIs) will usually contain explicit methods, though possibly not for replacement.
如果没有找到方法,as看起来为一体。首先,如果任Class或class(object)是其他超类定义将包含构建一个要挟的方法所需的信息。在通常情况下,子类包含超(即其所有插槽),构造方法提取或替换继承的插槽。非简单的扩展(的结果调用setIs一个)通常将包含明确的方法,虽然可能不更换。

If no subclass/superclass relationship provides a method, as looks for an inherited method, but applying, inheritance for the argument from only, not for the argument to (if you think about it, you'll probably agree that you wouldn't want the result to be from some class other than the Class specified). Thus, selectMethod("coerce", sig, useInherited= c(from=TRUE, to= FALSE)) replicates the method selection used by as().
如果没有子类/超的关系提供了一种方法,as看起来继承的方法,但申请继承的说法from只,而不是参数to(如果你觉得它,你可能会同意,你会不会想的结果是比Class指定)的其他一些类。因此,selectMethod("coerce", sig, useInherited= c(from=TRUE, to= FALSE))复制as()使用方法的选择。

In nearly all cases the method found in this way will be cached in the table of coerce methods (the exception being subclass relationships with a test, which are legal but discouraged). So the detailed calculations should be done only on the first occurrence of a coerce from class(object) to Class.
在几乎所有情况下,发现这样的方法将是在要挟的方法表缓存(异常子类的关系与测试,这是合法的,但泄气)。因此,应做详细的计算,只上要挟从class(object)Class第一次出现的。

Note that  coerce is not a standard generic function.  It is not intended to be called directly.  To prevent accidentally caching an invalid inherited method, calls are routed to an equivalent call to as, and a warning is issued.  Also, calls to selectMethod for this function may not represent the method that as will choose.  You can only trust the result if the corresponding call to as has occurred previously in this session.
注意coerce是不是一个标准通用的功能。它不打算直接调用。缓存无效继承的方法,以防止意外,呼叫路由到相当于调用as,并发出警告。此外,selectMethodas会选择本功能可能无法代表该方法的调用。如果相应的调用as发生在本次会议之前,你只能相信结果。

With this explanation as background, the function setAs does a fairly obvious computation:  It constructs and sets a method for the function coerce with signature c(from, to), using the def argument to define the body of the method.  The function supplied as def can have one argument (interpreted as an object to be coerced) or two arguments (the from object and the to class).  Either way, setAs constructs a function of two arguments, with the second defaulting to the name of the to class.  The method will be called from as with the object as the from argument and no to argument, with the default for this argument being the name of the intended to class, so the method can use this information in messages.
这个解释为背景,功能setAs做了相当明显的计算,它的构造和功能coerce签名c(from, to),def参数设置方法定义方法的主体。 def提供的功能可以有一个参数(解释作为一个被胁迫的对象)或两个参数(from对象和to类)。无论哪种方式,setAs构造函数的两个参数,第二拖欠to类的名称。该方法将被称为asfrom论点,并没有to参数的对象,这是拟to类的名称参数的默认,因此该方法可以在邮件中使用此信息。

The direct version of the as function also has a strict= argument that defaults to TRUE. Calls during the evaluation of methods for other functions will set this argument to FALSE. The distinction is relevant when the object being coerced is from a simple subclass of the to class; if strict=FALSE in this case, nothing need be done. For most user-written coerce methods, when the two classes have no subclass/superclass, the strict= argument is irrelevant.
as功能的直接版本也有一个strict=参数默认为TRUE。在呼吁评估等功能的方法,将设置此参数FALSE。区别是有关时,被强制的对象是从一个简单的to类的子类;如果strict=FALSE在这种情况下,没有什么需要做。对于大多数用户编写的要挟方法,当两个类没有子类/超,strict=参数是无关紧要的。

The replace argument to setAs provides a method for `coerce<-`. As with all replacement methods, the last argument of the method must have the name value for the object on the right of the assignment. As with the coerce method, the first two arguments are from, to; there is no strict= option for the replace case.
“replace参数setAs提供了coerce<-方法。与所有的替代方法,该方法的最后一个参数必须有value转让权利的对象的名称。与coerce方法,前两个参数是from, to有没有strict=替换情况的选项。

The function coerce exists as a repository for such methods, to be selected as described above by the as function.  Actually dispatching the methods using standardGeneric could produce incorrect inherited methods, by using inheritance on the to argument; as mentioned, this is not the logic used for as. To prevent selecting and caching invalid methods, calls to coerce are currently mapped into calls to as, with a warning message.
这种方法库的功能coerce存在,被选中上面的as功能描述。其实调度使用standardGeneric可能产生不正确继承的方法,通过使用继承to参数;提到的方法,这是不是逻辑as。为了防止选择和缓存无效的方法,调用coerce目前映射到调用as,一条警告消息。


基本的强制方法----------Basic Coercion Methods----------

Methods are pre-defined for coercing any object to one of the basic datatypes.  For example, as(x, "numeric") uses the existing as.numeric function.  These built-in methods can be listed by showMethods("coerce").
方法是预先定义的强迫任何对象的基本数据类型之一。例如,as(x, "numeric")使用现有的as.numeric功能。这些内置的方法,可以列出showMethods("coerce")。


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

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

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

If you think of using try(as(x, cl)), consider canCoerce(x, cl) instead.
如果你想使用try(as(x, cl)),考虑canCoerce(x, cl)代替。


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


## using the definition of class "track" from \link{setClass}[#使用类的“轨道”的定义,从\链接{setClass}]



setAs("track", "numeric", function(from) from@y)

t1 <- new("track", x=1:20, y=(1:20)^2)

as(t1, "numeric")

## The next example shows:[#下面的例子说明:]
##  1. A virtual class to define setAs for several classes at once.[#1。一个虚拟的类定义为几类setAs一次。]
##  2. as() using inherited information[#2。 ()使用遗传信息]

setClass("ca", representation(a = "character", id = "numeric"))

setClass("cb", representation(b = "character", id = "numeric"))

setClass("id")
setIs("ca", "id")
setIs("cb", "id")


setAs("id", "numeric", function(from) from@id)

CA <- new("ca", a = "A", id = 1)
CB <- new("cb", b = "B", id = 2)

setAs("cb", "ca", function(from, to )new(to, a=from@b, id = from@id))

as(CB, "numeric")



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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-24 05:27 , Processed in 0.024171 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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