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

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

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

                                        Objects With Fields Treated by Reference (OOP-style)
                                         参考设计(OOP风格)治疗领域的对象

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

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

The software described here supports reference classes whose objects have fields accessed by reference in the style of “OOP” languages such as Java and C++. Computations with these objects invoke methods on them and extract or set their fields. The field and method computations potentially modify the object. All computations referring to the objects see the modifications, in contrast to the usual functional programming model in R. Reference classes can be used to program in R directly or in combination with an interface to an OOP-style language, allowing R-written methods to extend the interface.
这里描述的软件支持引用类的对象通过引用访问字段中的“面向对象”的语言,如Java和C风格+。计算这些对象调用它们的方法和提取或设置各自的领域。字段和方法计算潜在的对象进行修改。所指的对象全部计算修改,对比度通常在R参考类功能的编程模型可以用来设计研发直接或结合与一个面向对象的语言风格的接口,允许R  - 写方法扩展接口。


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


setRefClass(Class, fields = , contains = , methods =,
     where =, ...)

getRefClass(Class, where =)



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

参数:Class
character string name for the class.  In the call to getRefClass() this argument can also be any object from the relevant class;  note also the corresponding reference class methods documented in the section on “Writing Reference Methods”.  
字符类的字符串名称。在调用getRefClass()这种说法也可以是任何有关的类的对象;还注意到相应的参考级“编写参考方法”一节中介绍的方法。


参数:fields
either a character vector of field names or a named list of the fields.  The resulting fields will be accessed with reference semantics (see the  section on “Reference Objects”).  If the argument is a list, the elements of the list can be the character string name of a class, in which case the field must be from that class or a subclass.  The element in the list can alternatively be an accessor function, a function of one argument that returns the field if called with no argument or sets it to the value of the argument otherwise. Accessor functions are used internally and for inter-system interface applications. Their definition follows the rules for writing methods for the class: they can refer to other fields and can call other methods for this class or its superclasses. See the section on “Implementation” for the internal mechanism used by accessor functions.  Note that fields are distinct from the slots, if any, in the object.  Slots are, as always, handled by standard R object management.  Slots for the class can be included (as the representation= argument) in the ... argument.  
无论是矢量字符的字段名或字段命名列表。由此产生的领域,将要访问的引用语义(见“参考对象”一节)。如果参数是一个列表,列表中的元素可以是一个类的字符串名称,领域,在这种情况下,必须从这个类或子类。列表中的元素,也可以是一个存取函数,函数的一个参数,如果不带参数的调用返回的字段或设置它的参数的值,否则。存取函数内部使用和系统间接口应用。其定义如下的规则:编写类的方法可以参考到其他领域和其他方法可以调用这个类或其超。见节存取函数所使用的内部机制“实施”。请注意,字段是鲜明的插槽,如果有的话,在对象。插槽,一如既往地处理标准的R对象管理。类的插槽可以包含(representation=论点)在...参数。


参数:contains
optional vector of superclasses for this class.  If a superclass is also a reference class, the fields and class-based methods will be inherited.  
这一类的超类的可选向量。如果一个超也是一个引用类,字段和基于类的方法将被继承。


参数:methods
a named list of function definitions that can be invoked on objects from this class.  These can also be created by invoking the $methods method on the generator object returned.  See the section on “Writing Reference Methods” for details.  Two optional method names are interpreted specially, initialize and finalize. If an initialize method is defined, it will be invoked when an object is generated from the class.  See the discussion of method $new(...)  in the section “Reference Object Generators”.  If a finalize method is defined, a function will be registered to invoke it before the environment in the object is discarded by the garbage collector. See the matrix viewer example for both initialize and finalize methods.
命名的功能,可以从这个类的对象上调用定义列表。这些也可以由发生器返回的对象上调用$methods方法。看到“写作参考方法”一节。两个可选的方法名称是专门解释,initialize和finalize。如果initialize方法的定义,它会被调用时从类生成一个对象。见的方法$new(...)一节中的“参考对象”发电机“的讨论。如果finalize方法的定义,函数将被登记前被丢弃的垃圾收集器的环境中的对象调用它。初始化和终结方法矩阵观众例子。


参数:where
the environment in which to store the class definition.  Defaults to the package namespace or environment for code that is part of an R package, and to the global environment for code sourced directly at the session top level.  
所处的环境来存储类的定义。默认的代码包的命名空间或环境是R包的一部分,并出席了本届会议的高层直接采购的代码为全球环境。


参数:...
other arguments to be passed to setClass.  
其他参数被传递到setClass。


值----------Value----------

setRefClass and getRefClass both return a generator object for the class. This is itself a reference object, with methods to generate objects from the class and also for defining new methods and for help-style documentation. See the section on “Reference Class Generator Objects” for details. Note that Class in the call to getRefClass() can be an object from the corresponding class, and that a similar reference class method $getRefClass()  is available as well.
setRefClass和getRefClass都返回一个类生成对象。这本身就是一个参考对象,方法,类和定义新的方法和风格帮助文件生成的对象。见“参考类生成对象”一节。请注意,Class调用getRefClass()在可以从相应的类的对象,类似的引用类的方法$getRefClass()以及。

setRefClass defines the class and stores its class definition. getRefClass requires that the class has been defined as a reference class.
setRefClass定义类和存储的类的定义。 getRefClass要求类已作为参考类的定义。


参考对象----------Reference Objects----------

Normal objects in R are passed as arguments in function calls consistently with functional programming semantics; that is, changes made to an object passed as an argument are local to the function call.  The object that supplied the argument is unchanged.
在R的正常对象传递函数的参数调用始终与功能编程的语义,即,作为一个参数传递的对象所做的更改是本地函数调用。提供的参数的对象是不变的。

The functional model (sometimes called pass-by-value) is suitable for many statistical computations and is implicit, for example, in the basic R software for fitting statistical models. In some other situations, one would like all the code dealing with an object to see the exact same content, so that changes made in any computation would be reflected everywhere. This is often suitable if the object has some “objective” reality, such as a window in a user interface.
许多统计计算功能模型(有时也被称为按值传递)是合适的,是隐性的,例如在统计模型拟合的基本R软件。在其他一些情况下,希望所有的代码处理对象看到相同的内容,所以,在任何计算中所做的更改将处处体现。这往往是合适的,如果对象有一些“客观”的现实,如在用户界面的窗口。

In addition, commonly used languages, including Java, C++ and many others, support a version of classes and methods assuming reference semantics. The corresponding programming mechanism is to invoke a method on an object. In the R syntax that we use for this operation, one invokes a method, m1 say, on an object x by the expression x$m1(...).
此外,常用语言,包括Java,C + +和许多人一样,支持假设引用语义类和方法的一个版本。相应的编程机制是一个对象上调用方法。在R的语法,我们使用此操作,调用一个方法,对象m1表达xx$m1(...)说,。

Methods in this paradigm are associated with the object, or more precisely with the class of the object, as opposed to methods in a function-based class/method system, which are fundamentally associated with the function (in R, for example, a generic function in an R session has a table of all its currently known methods). In this document “methods for a class” as opposed to “methods for a function” will make the distinction.
这个范例中的方法与对象相关联,或更准确地与对象类,而不是基于函数的类/方法的系统,这是从根本上相关的功能(在R,例如,一个普通的方法在R会话的功能,其所有目前已知的方法有一个表)。在这份文件中,而不是“函数的方法”,“一类的方法”的区别。

Objects in this paradigm usually have named fields on which the methods operate. In the R implementation, the fields are defined when the class is created. The field itself can optionally have a specified class, meaning that only objects from this class or one of its subclasses can be assigned to the field. By default, fields have class "ANY". Fields may also be defined by supplying an accessor function which will be called to get or set the field. Accessor functions are likely when reference classes are part of an inter-system interface. The interface will usually supply the accessor functions automatically based on the definition of the corresponding class in the other language.
在这个范例中的对象通常命名上的方法经营的领域。在R实现,字段定义类时创建的。字段本身可以选择指定的类,这意味着只有从这个类或其子类之一的对象可以被分配到外地。默认情况下,领域类"ANY"。领域也可以通过提供一个存取函数将被调用,以获取或设置字段定义。存取功能有可能时引用类是一个跨系统接口的一部分。接口通常会提供的存取功能,自动在相应的其他语言类的定义的基础。

Fields are accessed by reference. In particular, invoking a method may modify the content of the fields.
访问字段参考。特别是,调用一个方法可能修改字段的内容。

Programming for such classes involves writing new methods for a particular class. In the R implementation, these methods are R functions, with zero or more formal arguments. The object on which the methods are invoked is not an explicit argument to the method. Instead, fields and methods for the class can be referred to by name in the method definition. The implementation uses R environments to make fields and methods available by name. Additional special fields allow reference to the complete object and to the definition of the class.  See the section on “Inheritance”.
这样的类的编程涉及一个特定的类编写的新方法。在R的实施,这些方法是R的功能,与零个或多个形式参数。该方法被调用的对象是没有一个明确的参数的方法。相反,类的字段和方法可以被称为在方法定义的名称。实施使用R环境,使字段和方法的名字。额外的特殊领域,允许参考完整的对象和类的定义。见“继承”一节。

The goal of the software described here is to provide a uniform programming style in R for software dealing with reference classes, whether implemented directly in R or through an interface to one of the OOP languages.
这里描述的软件的目标是提供一个统一的编程风格,研发与引用类的软件处理,无论是R或直接通过接口实现的OOP语言之一。


编写参考方法----------Writing Reference Methods----------

Reference methods are functions supplied as elements of a named list, either when invoking g$methods()  on a generator object g or as the argument methods in a call to setRefClass. They are written as ordinary R functions but have some special features and restrictions. The body of the function can contain calls to any other reference method, including those inherited from other reference classes and may refer to fields in the object by name.
参考方法是提供一个名为列表中的元素的功能,当调用g$methods()发电机对象g或作为参数methods在调用setRefClass。作为普通R函数都写,但有一些特殊的功能和限制。函数体可以包含调用任何其他的参考方法,包括那些从其他参考类继承,可参考对象的字段的名字。

Fields may be modified in a method by using the non-local assignment operator, <<-, as in the $edit and $undo methods in the example below. Note that non-local assignment is required:  a local assignment with the <- operator just creates a local object in the function call, as it would in any R function. When methods are installed, a heuristic check is made for local assignments to field names and a warning issued if any are detected.
字段可能被修改的方法使用的非本地赋值运算符,<<-,$edit和$undo方法在下面的例子。请注意,非本地分配与地方分配的只是创建<-运营商在函数调用本地对象,因为它会在任何R的功能。安装方法时,字段名称和发出警告,如果发觉有任何地方分配的启发式检查。

Reference methods should be kept simple; if they need to do some specialized R computation, that computation should use a separate R function that is called from the reference method. Specifically, methods can not use special features of the enclosing environment mechanism, since the method's environment is used to access fields and other methods. Reference methods can not themselves be generic functions; if you want additional function-based method dispatch, write a separate generic function and call that from the method.
参考方法应尽量简单,如果他们需要做一些专门的研发计算,计算应使用一个单独的参考方法称为R函数。具体来说,方法不能使用封闭的环境机制的特殊功能,因为该方法的环境,是用来访问领域和其他方法。参考方法不能本身是通用功能;如果你想附加功能的方法调度,写一个单独的通用功能和调用的方法,从。

The entire object can be referred to in a method by the reserved name .self, as shown in the save= method of the example. The special object .refClassDef contains the definition of the class of the object. These fields are read-only (it makes no sense to modify these references), with one exception. The .self field can be modified in the $initialize  method, because the object is still being created at this stage. (Assignments to this field, as to all fields, need to use the non-local assignment operator.) This is the preferred way to assign S4-style attributes to the object, if you need to do so; but remember that these attributes will not behave according to reference semantics, unlike fields.
整个对象可以在一个方法,保留的名称.self,save=方法的例子所示。特殊对象.refClassDef包含对象的类的定义。这些字段是只读(修改这些引用,它是没有意义的),但有一个例外。 .self场$initialize方法可以修改,因为对象仍在此阶段创建。 (分配到这一领域,各个领域,需要使用非本地赋值运算符)。这是首选的方式给中S4中样式属性的对象,如果你需要这样做;但要记住,这些属性将根据引用语义行为,不同领域。

The methods available include methods inherited from superclasses, as discussed in the next section.
可用的方法包括从父类继承的方法,如在下一节讨论。

Documentation for the methods can be obtained by the $help  method for the generator object. Methods for classes are not documented in the Rd format used for R functions. Instead, the $help  method prints the calling sequence of the method, followed by self-documentation from the method definition, in the style of Python. If the first element of the body of the method is a literal character string (possibly multi-line), that string is interpreted as documentation. See the method definitions in the example.
$help方法可以得到文件的方法,发电机对象。不记录类的方法RdR函数使用的格式。相反,$help方法打印,文件从定义的方法,在Python的风格,自我的方法调用序列。如果方法体的第一个元素是文字字符串(可能多行),该字符串被解释为文件。例子中的方法定义。


继承权----------Inheritance----------

Reference classes inherit from other reference classes by using the standard R inheritance; that is, by including the superclasses in the contains= argument when creating the new class. Non-reference classes can also be included in the contains= argument.  The class definition mechanism treats reference and non-reference superclasses slightly differently. If the contained reference classes themselves have reference superclasses, these will be moved ahead of any non-reference superclasses in the class definition (otherwise the ordering of superclasses may be ambiguous). The names of the reference superclasses are in slot refSuperClasses of the class definition.
引用类继承自其他引用类使用标准的R继承,也就是说,包括在contains=参数的父类,创建新类时,。 contains=参数,也可以包括在非参考类。类定义的机制,将引用和非引用父略有不同。如果包含引用类本身具有一定的参考父,将提前在类定义(否则父的顺序可能是模棱两可的)任何非参考父。引用父类的名称是在插槽refSuperClasses类定义的。

Class fields are inherited.  A class definition can override a field of the same name in a superclass only if the overriding class is a subclass of the class of the inherited field.  This ensures that a valid object in the field remains valid for the superclass as well.
继承类的字段。 A类定义可以覆盖一个相同的名称在一个超领域,只有在重要的类是继承字段的类的一个子类。这将确保在该领域的有效对象超类以及仍然有效。

Inherited methods are installed in the same way as directly specified methods. The code in a method can refer to  inherited methods in the same way as directly specified methods.
继承的方法是直接指定的方法相同的方式安装。在方法中的代码可以参考,在同样的方式方法直接指定继承的方法。

A method may override a method of the same name in a superclass. The overriding method can call the superclass method by callSuper(...) as described below.
一种方法可能会覆盖在一个超类的名称相同的方法。压倒一切的方法可以调用父类的方法callSuper(...)如下所述。

All reference classes inherit from the class "envRefClass", which provides the following methods.
所有引用的类都继承自类"envRefClass",它提供了以下方法。

Calls the method inherited from a reference superclass. The call is meaningful only from within another method, and will be resolved to call the inherited method of the same name. The arguments to $callSuper  are passed to the superclass version. See the matrix viewer class in the example.
调用从一个引用父类继承的方法。通话只能在另一种方法是有意义的,并会得到解决的同名调用继承的方法。的$callSuper的参数被传递给父类的版本。在这个例子中看到的矩阵浏览器类。

Note that the intended arguments for the superclass method must be supplied explicitly; there is no convention for supplying the arguments automatically, in contrast to the similar mechanism for functional methods.
请注意,必须提供明确预期的父类的方法参数;提供的参数,自动对比功能的方法类似的机制,有没有约定。

Creates a copy of the object.  With reference classes, unlike ordinary R objects, merely assigning the object with a different name does not create an independent copy.  If shallow is FALSE, any field that is itself a reference object will also be copied, and similarly recursively for its fields.  Otherwise, while reassigning a field to a new reference object will have no side effect, modifying such a field will still be reflected in both copies of the object. The argument has no effect on non-reference objects in fields.  When there are reference objects in some fields but it is asserted that they will not be modified, using shallow = TRUE will save some memory and time.
创建一个对象的副本。参考类,不同于普通的R对象,只是分配的对象,用不同的名称,不建立一个独立的副本。如果shallow是FALSE,任何领域,本身就是一个引用对象也将被复制,同样递归其领域。而重新分配一个字段到一个新的参考对象,否则将无任何副作用,修改,这样的字段仍然会体现在对象的两个副本。参数没有效果领域中的非参考对象。当有在某些领域的参考对象,但它是断言,他们将不会被修改,使用shallow = TRUE将节省一些内存和时间。

With one argument, returns the field of the object with character string name.  With two arguments, the corresponding field is assigned value.  Assignment checks that name specifies a valid field, but the single-argument version will attempt to get anything of that name from the object's environment.
有一个参数,返回字符串name对象的领域。有两个参数,分配相应的字段value。分配检查name指定一个有效的领域,但在单参数版本将尝试获得该名称的任何对象的环境。“

The $field()  method replaces the direct use of a field name, when the name of the field must be calculated, or for looping over several fields.
$field()方法取代直接使用字段名称,字段的名称时,必须计算,或在多个领域的循环。

Returns the result of coercing the object to Class (typically one of the superclasses of the object's class).  Calling the method has no side effect on the object itself.
返回胁迫对象的结果Class(通常是一个对象的类的超类)。调用方法的对象本身有没有副作用。

These return respectively the generator object and the formal class definition for the reference class of this object, efficiently.
这些对象分别发电机和正式的类定义这个对象的引用类,高效的回报。

Import the object value into the current object, replacing the corresponding fields in the current object. Object value must come from one of the superclasses of the current object's class. If argument Class is supplied, value is first coerced to that class.
导入对象value到当前对象替换当前对象的相应字段。对象value必须从当前对象的类的超类之一。如果参数Class提供,value第一次被强制这一类。

Initialize the fields of the object from the supplied arguments.  This method is usually only called from a class with a $initialize() method.  It corresponds to the default initialization for reference classes.  If there are slots and non-reference superclasses, these may be supplied in the ... argument as well.
对象提供的参数初始化字段。这种方法通常只称为一类与$initialize()方法。它对应的参考类的默认初始化。如果有槽和非引用父类,这些可提供...论点。

Typically, a specialized $initialize() method carries out its own computations, then invokes $initFields() to perform standard initialization, as shown in the matrixViewer class in the example below.
通常情况下,一个专门的$initialize()方法进行了自己的计算,然后调用$initFields()执行标准初始化,如matrixViewer类在下面的例子所示。

This method is called when the object is printed automatically, analogously to the show function.  A general method is defined for class "envRefClass".  User-defined reference classes will often define their own method: see the Example below.
这种方法被称为对象时,自动打印,类似show功能。一个通用的方法被定义为类的"envRefClass"。用户定义的参考类通常会定义自己的方法:请看下面的例子。

Note two points in the example.  As with any show() method, it is a good idea to print the class explicitly to allow for subclasses using the method.  Second, to call the function show() from the method, as opposed to the $show()  method itself, refer to methods::show() explicitly.
请注意两个点的例子。任何show()方法,它是一个好主意,打印类,明确允许使用的方法的子类。其次,要调用函数show()的方法,而不是$show()方法本身,是指methods::show()明确。

Apply the tracing and debugging facilities of the trace function to the reference method what.
申请的参考方法tracewhat函数的跟踪和调试设施。

All the arguments of the trace function can be supplied, except for signature, which is not meaningful.
除了tracesignature函数的所有参数可以提供,这是没有意义的。

The reference method can be invoked on either an object or the generator for the class.  See the section on Debugging below for details.
参考方法可以调用一个对象或类的发电机。看到下面的详细信息,在调试的部分。

Objects also inherit two reserved fields:
对象也继承两个保留的领域:

a reference to the entire object;
整个对象的引用;

the class definition.
类的定义。

The defined fields should not override these, and in general it is unwise to define a field whose name begins with ".", since the implementation may use such names for special purposes.
定义的字段不应该重写这些,一般是不明智的,定义一个字段的名称与"."开始实施以来,可以使用特殊用途等的名字。


参考类生成对象----------Reference Class Generator Objects----------

The call to setRefClass defines the specified class and returns a &ldquo;generator&rdquo; object for that class. The generator object is itself a reference object (of class "refObjectGenerator"). Its fields are def, the class definition, and className, the character string name of the class.
setRefClass呼叫定义指定的类,并返回该类的“发电机”的对象。发电机的对象本身就是一个参考对象(类"refObjectGenerator")。它的领域是def,类的定义,和className“之类的字符串名称。

Methods for generator objects exist to generate objects from the class, to access help on reference methods, and to define new reference methods for the class. The currently available methods are:
发电机对象的方法存在生成的类的对象,访问的参考方法的帮助,并定义为类新的参考方法。目前可用的方法是:

This method is equivalent to the function new with the class name as an argument.  The ... arguments are values for the named fields. If the class has a method defined for $initialize(),  this method will be called once the reference object has been created.  You should write such a method for a class that needs to do some special initialization. In particular, a reference method is recommended rather than a method for the S4 generic function initialize(), because some special initialization is required for reference objects before the initialization of fields. As with S4 classes, methods are written for $initialize()  and not for $new(),  both for the previous reason and also because $new()  is invoked on the generator object and would be a method for that class.
这种方法是等效的功能new作为参数的类的名称。 “......参数是命名字段的值。如果类有$initialize()定义方法,该方法将被调用一次的参考对象已经创建。你应该写这样一个类,需要做一些特殊的初始化方法。参考方法,特别是为S4的通用功能的方法,而不是建议initialize(),因为一些特殊的初始化之前初始化字段的参考对象。S4类,方法的书面$initialize()不$new(),无论是前面的原因,也因为$new()发电机的对象上,将是这个类的方法调用。

The default method for $initialize()  is equivalent to invoking the method $initFields(...).  Named arguments assign initial values to the corresponding fields. Unnamed arguments must be objects from this class or a reference superclass of this class. Fields will be initialized to the contents of the fields in such objects, but named arguments override the corresponding inherited fields. Note that fields are simply assigned.  If the field is itself a reference object, that object is not copied. The new and previous object will share the reference. Also, a field assigned from an unnamed argument counts as an assignment for locked fields. To override an inherited value for a locked field, the new value must be one of the named arguments in the initializing call. A later assignment of the field will result in an error.
$initialize()的默认方法是相当于调用的方法$initFields(...)。命名参数分配到相应的字段的初始值。未命名的参数必须是从这个类的参考类或超类对象。字段将被初始化这些对象领域的内容,但命名参数覆盖相应的继承领域。注意只是分配领域。如果该字段本身就是一个参考对象,该对象不会被复制。新的和以前的对象将分享参考。此外,从无名参数计数作为一个锁定的领域分配分配一个字段。锁定的领域要重写继承的值,新值必须在初始化调用的命名参数之一。后分配的领域,将导致错误。

For technical reasons, the default method does not currently appear explicitly, but can be invoked by $callSuper(...)  from a method for $initialize().  Initialization methods need some care in design, as they do for S4 classes. In particular, remember that others may subclass your class and pass through field assignments or other arguments.  Therefore, your method should normally include ... as an argument, all other arguments should have defaults or check for missingness, and your method should pass all initialized values on via $callSuper() or $initFields() if you know that your superclasses have no initialization methods.
由于技术上的原因,默认的方法目前没有出现明确的,但可以由$callSuper(...)从$initialize()方法调用。初始化方法需要一些在设计上的照顾,他们为中S4中类。特别是,记得别人也可以通过外派或其他参数的类,并通过子类。因此,你的方法,通常应包括...作为一个参数,其他参数都应该有违约或检查missingness,和你的方法,应该通过所有初始值通过$callSuper()或$initFields()如果你知道你的父有没有初始化方法。

Prints brief help on the topic.  The topics recognized are reference method names, quoted or not.
打印简短的帮助主题。公认的主题是参考方法的名称,引用或没有。

The information printed is the calling sequence for the method, plus self-documentation if any. Reference methods can have an initial character string or vector as the first element in the body of the function defining the method. If so, this string is taken as self-documentation for the method (see the section on &ldquo;Writing Reference Methods&rdquo; for details).
印刷信息是方法的调用序列,再加上,如果没有自我文档。参考方法可以有一个初步的字符串或向量函数的定义方法体中的第一个元素。如果是这样,这个字符串被当作自文档的方法(见“编写参考方法”部分的详细内容)。

If no topic is given or if the topic is not a method name, the definition of the class is printed.
如果没有主题,如果主题是不是一个方法的名称,类的定义打印。

With no arguments, returns a list of the reference methods for this class.
不带任何参数,返回一个列表,这个类的参考方法。

Named arguments are method definitions, which will be installed in the class, as if they had been supplied in the methods argument to setRefClass(). Supplying methods in this way, rather than in the call to setRefClass(), is largely for the sake of clearer source code when many or large methods are being defined. All methods for a class should be defined in the source code that defines the class, typically as part of a package. In particular, methods can not be redefined in a class in an attached package with a namespace: The class method checks for a locked binding of the class definition.
命名参数是方法的定义,将被安装在课堂上,如果他们已经在提供methodssetRefClass()参数。在这种方式提供的方法,而不是在调用setRefClass(),主要是为了更清晰的源代码时,许多或大方法被定义。一类所有的方法应该被定义在源代码中定义的类,通常是作为一个包的一部分。特别是,方法不能被重新定义在同一个命名空间的附加包类:锁定一个类定义绑定类的方法检查。

The new methods can refer to any currently defined method by name (including other methods supplied in this call to $methods().  Note though that previously defined methods are not re-analyzed meaning that they will not call the new method (unless it redefines an existing method of the same name).
新的方法可以参考名称(包括调用$methods()注意在此提供的其他方法,尽管先前定义的方法,重新分析,这意味着它们不会调用新的方法,目前定义的任何方法(除非它重新定义现有的同名方法)。

To remove a method, supply NULL as its new definition.
要删除的方法,提供NULL作为其新的定义。

Returns a list of the fields, each with its corresponding class. Fields for which an accessor function was supplied in the definition have class "activeBindingFunction".
返回字段列表,每一个都有其相应的类。存取函数的定义中提供的字段类"activeBindingFunction"。

The fields named in the arguments are locked; specifically, after the lock method is called, the field may be set once.  Any further attempt to set it will generate an error.
在参数命名的字段被锁定;具体地说,锁方法被调用后,可设置一次实地。设置任何进一步尝试将产生一个错误。

If called with no arguments, the method returns the names of the locked fields.
如果不带参数的调用,该方法返回锁定的字段的名称。

Fields that are defined by an explicit accessor function can not be locked (on the other hand, the accessor function can be defined to generate an error if called with an argument).
是一个明确的存取功能定义的字段不能被锁定(另一方面,存取函数可以定义为产生一个错误,如果用一个参数调用)。

All code to lock fields should normally be part of the definition of a class; that is, the read-only nature of the fields is meant to be part of the class definition, not a dynamic property added later. In particular, fields can not be locked in a class in an attached package with a namespace:  The class method checks for a locked binding of the class definition.  Locked fields can not be subsequently unlocked.
所有代码锁定领域,一般应在类定义的一部分时,该字段是只读性质是类定义的一部分,而不是一个动态属性。领域,特别是,不能被锁定在一个附加的包类在同一个命名空间:锁定一个类定义绑定类的方法检查。锁定的字段不能被随后解除。

Establish a traced version of method what for objects generated from this class.  The generator object tracing works like the $trace() method for objects from the class, with two differences. Since it changes the method definition in the class object itself, tracing applies to all objects, not just the one on which the trace method is invoked.
建立一个追踪的方法版本what从这个类生成的对象。跟踪发电机的对象从类对象的像$trace()方法作品,有两个差异。方法定义中的类的对象本身,因为它改变,跟踪适用于所有对象,而不仅仅是跟踪方法被调用。

Second, the optional argument classMethod = TRUE allows tracing on the methods of the generator object itself, such as $new(). By default, what is interpreted as the name of a method in the class for which this object is the generator.
其次,可选参数classMethod = TRUE允许发电机对象本身的方法,如$new(),跟踪。默认情况下,what被解释为类中的方法,而这个对象是发电机的名称。

A number of systems using the OOP programming paradigm recommend or enforce getter and setter methods corresponding to each field, rather than direct access by name. In the R version presented here (and fairly often elsewhere as well), a field named abc of an object x would be extracted by x$getAbc() and assigned by x$setAbc(value). The $accessors  method is a convenience function that creates getter and setter methods for the specified fields.
一些系统使用面向对象编程范式建议或执行相应的每个字段的getter和setter方法,而不是直接通过名称访问。在这里(相当频繁,以及其他地方)的R版本,一场名为abc对象的xx$getAbc()提取和分配x$setAbc(value)。 $accessors方法是创建一个方便的功能,为指定的字段的getter和setter方法。


实施----------Implementation----------

Reference classes are implemented as S4 classes with a data part of type "environment". An object generated from a reference class has this type. Fields correspond to named objects in the environment. A field associated with an accessor function is implemented as an active binding. In addition, fields with a specified class are implemented as a special form of active binding to enforce valid assignment to the field. A field, say data, can be accessed generally by an expression of the form x$data  for any object from the relevant class. In a method for this class, the field can be accessed by the name data. A field that is not locked can be set by an expression of the form x$data <- value. Inside a method, a field can be assigned by an expresion of the form x <<- value. Note the non-local assignment operator. The standard R interpretation of this operator works to assign it in the environment of the object. If the field has an accessor function defined, getting and setting will call that function.
落实S4类引用类的类型"environment"的部分数据。这从一个引用类生成一个对象类型。字段对应环境中的命名对象。实施积极的约束力与存取功能相关的领域。此外,与指定类的领域,实施了积极的约束力,执行有效的分配到外地的特殊形式。一个字段,说data,可访问的形式表达一般x$data从有关类的任何对象。在这个类的方法,字段可以访问的名称data。一个未锁定的领域可以设置表达形式x$data <- value。在一个方法,一个字段可以分配形式x <<- value中表达。注意:非本地赋值运算符。该运营商的标准R的解释工作,分配对象的环境中。如果该字段有一个定义存取函数,获取和设置,将调用该函数。

When a method is invoked on an object, the function defining the method is installed in the object's environment, with the same environment as the environment of the function.
功能定义方法的对象上调用一个方法时,安装在对象的环境,与环境的功能相同的环境。


系统间的接口----------Inter-System Interfaces----------

A number of languages use a similar reference-based programming model with classes and class-based methods. Aside from differences in choice of terminology and other details, many of these languages are compatible with the programming style described here. R interfaces to the languages exist in a number of packages.
一些语言类和基础类的方法使用一个类似的参考基础的编程模型。除了选择术语和其他细节的差异,许多这些语言兼容这里所描述的编程风格。 R接口的语言中存在的包数量。

The reference class definitions here provide a hook for classes in the foreign language to be exposed in R. Access to fields and/or methods in the class can be implemented by defining an R reference class corresponding to classes made available through the interface. Typically, the inter-system interface will take care of the details of creating the R class, given a description of the foreign class (what fields and methods it has, the classes for the fields, whether any are read-only, etc.) The specifics for the fields and methods can be implemented via reference methods for the R class. In particular, the use of active bindings allows field access for getting and setting, with actual access handled by the inter-system interface.
参考类的定义在这里被暴露在R.访问字段和/或类中的方法,可以通过定义一个R参考类可通过该接口的类对应实施外语类提供了一个钩子。通常情况下,系统间的接口,将采取建立R类的细节照顾,给予外国类的说明(哪些领域和它的方法,字段的类,是否有只读等)实施的领域和方法的具体情况,可以通过R类的参考方法。积极绑定使用,特别是用于获取和设置与实际接入系统间的接口处理,允许现场访问。

R methods and/or fields can be included in the class definition as for any reference class. The methods can use or set fields and can call other methods transparently whether the field or method comes from the interface or is defined directly in R.
&#341;方法和/或领域可以包括任何参考类的类定义。可以使用的方法,或设置字段和透明域或方法是否来自接口或直接定义在R可以调用其他方法

For an inter-system interface using this approach, see the code for package Rcpp, version 0.8.7 or later.
对于一个跨系统,使用这种方法的接口,包Rcpp,0.8.7或更高版本的代码。


调试----------Debugging----------

The standard R debugging and tracing facilities can be applied to reference methods. Reference methods can be passed to debug and its relatives from an object to debug further method invocations on that object; for example, debug(xx$edit).
标准R调试和跟踪功能,可以应用到参考方法。参考方法可以从一个物体传递给debug和其亲属到该对象的方法调用调试,例如,debug(xx$edit)。

Somewhat more flexible use is available for a reference method version of the trace function. A corresponding $trace()  reference method is available for either an object or for the reference class generator (xx$trace() or mEdit$trace() in the example below). Using $trace() on an object sets up a tracing version for future invocations of the specified method for that object. Using $trace() on the generator for the class sets up a tracing version for all future objects from that class (and for all existing objects that have not yet invoked the traced method, because reference methods are cached lazily in the object when first invoked).
较为灵活的使用提供了trace函数的参考方法的版本。一个相应的$trace()参考方法是一个对象或参考类发生器(xx$trace()或mEdit$trace()在下面的例子)。使用$trace()对象设置为未来指定该对象的方法调用跟踪版本。使用$trace()发电机类设置跟踪的版本,从这个类的所有对象(和未来所有尚未调用跟踪的方法,因为参考的方法是缓存中的对象懒洋洋的现有对象时,首先调用)。

In either case, all the arguments to the standard  trace function are available, except for signature= which is meaningless since reference methods can not be S4 generic functions. This includes the typical style trace(what, browser) for interactive debugging and  trace(what, edit = TRUE) to edit the reference method interactively.
在这两种情况下,所有的参数标准trace功能是提供除了signature=这是毫无意义的,因为参考方法不能S4通用功能。这包括典型风格的交互式调试trace(what, browser)和trace(what, edit = TRUE)交互式编辑的参考方法。


作者(S)----------Author(s)----------



John Chambers




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


## a simple editor for matrix objects.  Method  $edit() changes some[#简单的编辑器为矩阵对象。方法$(编辑)改变一些]
## range of values; method $undo() undoes the last edit.[范围值;美元撤消()方法撤消最后一次编辑。]
mEdit <- setRefClass("mEdit",
      fields = list( data = "matrix",
        edits = "list"),
      methods = list(
     edit = function(i, j, value) {
       ## the following string documents the edit method[#以下字符串的文件编辑方法]
       'Replaces the range [i, j] of the
        object by value.
        '
         backup <-
             list(i, j, data[i,j])
         data[i,j] <<- value
         edits <<- c(edits, list(backup))
         invisible(value)
     },
     undo = function() {
       'Undoes the last edit() operation
        and update the edits field accordingly.
        '
         prev <- edits
         if(length(prev)) prev <- prev[[length(prev)]]
         else stop("No more edits to undo")
         edit(prev[[1]], prev[[2]], prev[[3]])
         ## trim the edits list[#修剪编辑列表]
         length(edits) <<- length(edits) - 2
         invisible(prev)
     },
     show = function() {
       'Method for automatically printing matrix editors'
       cat("Reference matrix editor object of class",
          classLabel(class(.self)), "\n")
       cat("Data: \n")
       methods::show(data)
       cat("Undo list is of length", length(edits), "\n")
     }
     ))

xMat <- matrix(1:12,4,3)
xx <- mEdit$new(data = xMat)
xx$edit(2, 2, 0)
xx
xx$undo()
mEdit$help("undo")
stopifnot(all.equal(xx$data, xMat))

utils::str(xx) # show fields and names of non-trivial methods[非平凡的方法显示领域和名称]

## add a method to save the object[#添加一个方法来保存对象]
mEdit$methods(
     save = function(file) {
       'Save the current object on the file
        in R external object format.
       '
         base::save(.self, file = file)
     }
)

tf <- tempfile()
xx$save(tf)


## Not run: [#无法运行:]
## Inheriting a reference class:  a matrix viewer[#继承参考类:矩阵观众]
mv <- setRefClass("matrixViewer",
    fields = c("viewerDevice", "viewerFile"),
    contains = "mEdit",
    methods = list( view = function() {
        dd <- dev.cur(); dev.set(viewerDevice)
        devAskNewPage(FALSE)
        matplot(data, main = paste("After",length(edits),"edits"))
        dev.set(dd)},
        edit = # invoke previous method, then replot[调用以前的方法,然后重绘]
          function(i, j, value) {
            callSuper(i, j, value)
            view()
          }))

## initialize and finalize methods[#初始化和Finalize方法]
mv$methods( initialize =
  function(file = "./matrixView.pdf", ...) {
    viewerFile <<- file
    pdf(viewerFile)
    viewerDevice <<- dev.cur()
    dev.set(dev.prev())
    callSuper(...)
  },
  finalize = function() {
    dev.off(viewerDevice)
  })

## debugging an object: call browser() in method $edit()[#调试对象:调用浏览器()方法在编辑()]
xx$trace(edit, browser)

## debugging all objects from class mEdit in method $undo()[#调试类的所有对象方法mEdit的美元撤消()]
mEdit$trace(undo, browser)

## End(Not run)[#结束(不运行)]


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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-23 02:02 , Processed in 0.029516 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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