| 60325 |
ripley |
1 |
% File src/library/methods/man/refClass.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
|
|
3 |
% Copyright 2010-2 R Core Team
|
|
|
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 52816 |
jmc |
6 |
\name{ReferenceClasses}
|
| 56985 |
maechler |
7 |
\title{Objects With Fields Treated by Reference (OOP-style)}
|
| 56186 |
murdoch |
8 |
\alias{ReferenceClasses}
|
| 52816 |
jmc |
9 |
\alias{setRefClass}
|
| 53003 |
jmc |
10 |
\alias{getRefClass}
|
| 52960 |
jmc |
11 |
\alias{initFieldArgs}
|
| 57841 |
jmc |
12 |
\alias{initRefFields}
|
| 52960 |
jmc |
13 |
\alias{activeBindingFunction-class}
|
|
|
14 |
\alias{defaultBindingFunction-class}
|
|
|
15 |
\alias{uninitializedField-class}
|
| 52816 |
jmc |
16 |
\alias{refClassRepresentation-class}
|
| 52903 |
jmc |
17 |
\alias{refObjectGenerator-class}
|
| 61058 |
jmc |
18 |
\alias{refGeneratorSlot-class}
|
| 52816 |
jmc |
19 |
\alias{refClass-class}
|
|
|
20 |
\alias{refObject-class}
|
| 52903 |
jmc |
21 |
\alias{refMethodDef-class}
|
| 55578 |
jmc |
22 |
\alias{refMethodDefWithTrace-class}
|
| 52846 |
jmc |
23 |
\alias{SuperClassMethod-class}
|
| 56335 |
jmc |
24 |
\alias{show,envRefClass-method}
|
| 52903 |
jmc |
25 |
\alias{show,refMethodDef-method}
|
| 52816 |
jmc |
26 |
\alias{show,refClassRepresentation-method}
|
|
|
27 |
\description{
|
| 53003 |
jmc |
28 |
The software described here supports reference classes whose objects have
|
|
|
29 |
fields
|
|
|
30 |
accessed by reference in the style of \dQuote{OOP} languages such as Java and
|
|
|
31 |
C++.
|
|
|
32 |
Computations with these objects invoke methods on them and
|
|
|
33 |
extract or set their fields.
|
|
|
34 |
The field and method computations potentially modify the object.
|
|
|
35 |
All computations referring to the objects see the modifications, in contrast to
|
|
|
36 |
the usual functional programming model in \R.
|
|
|
37 |
Reference classes can be used to program in \R directly or in combination
|
|
|
38 |
with an interface to an OOP-style language, allowing \R-written methods to
|
| 52903 |
jmc |
39 |
extend the interface.
|
| 52816 |
jmc |
40 |
}
|
|
|
41 |
\usage{
|
| 52960 |
jmc |
42 |
setRefClass(Class, fields = , contains = , methods =,
|
|
|
43 |
where =, ...)
|
| 52816 |
jmc |
44 |
|
| 53003 |
jmc |
45 |
getRefClass(Class, where =)
|
| 52816 |
jmc |
46 |
}
|
|
|
47 |
\arguments{
|
|
|
48 |
\item{Class}{
|
|
|
49 |
character string name for the class.
|
| 53669 |
jmc |
50 |
|
|
|
51 |
In the call to \code{getRefClass()} this argument can also be any
|
|
|
52 |
object from the relevant class; note also the corresponding reference
|
|
|
53 |
class methods documented in the section on \dQuote{Writing Reference Methods}.
|
| 52816 |
jmc |
54 |
}
|
| 52960 |
jmc |
55 |
\item{fields}{
|
|
|
56 |
either a character vector of field names or
|
|
|
57 |
a named list of the fields. The resulting fields will be accessed with reference semantics (see
|
| 52985 |
jmc |
58 |
the section on \dQuote{Reference Objects}). If the argument is a list, the
|
| 52960 |
jmc |
59 |
elements of the list can be the character string name of a class, in
|
|
|
60 |
which case the field must be from that class or a subclass.
|
|
|
61 |
|
| 53003 |
jmc |
62 |
The element in the list can alternatively be an \emph{accessor
|
|
|
63 |
function}, a function of one argument that returns
|
|
|
64 |
the field if called with no argument or sets it to the value of the
|
|
|
65 |
argument otherwise.
|
|
|
66 |
Accessor functions are used internally and for inter-system interface
|
|
|
67 |
applications.
|
|
|
68 |
Their definition follows the rules for writing methods for the class:
|
|
|
69 |
they can refer to other fields and can call other methods for this
|
|
|
70 |
class or its superclasses.
|
|
|
71 |
See the section on \dQuote{Implementation} for the internal mechanism
|
|
|
72 |
used by accessor functions.
|
| 52960 |
jmc |
73 |
|
|
|
74 |
Note that fields are distinct from
|
| 52816 |
jmc |
75 |
the slots, if any, in the object. Slots are, as always, handled by
|
| 62255 |
jmc |
76 |
standard \R{} object management.
|
|
|
77 |
It is not generally a good idea to mix slots and fields in the same
|
|
|
78 |
class; this confuses the distinction in behavior between reference
|
|
|
79 |
classes and regular S4 classes. See the comments in the
|
|
|
80 |
\dQuote{Implementation} section.
|
| 52816 |
jmc |
81 |
}
|
|
|
82 |
\item{contains}{
|
|
|
83 |
optional vector of superclasses for this class. If a superclass is
|
| 52846 |
jmc |
84 |
also a reference class, the fields and class-based methods will be inherited.
|
| 52816 |
jmc |
85 |
}
|
| 52960 |
jmc |
86 |
\item{methods}{
|
| 52816 |
jmc |
87 |
a named list of function definitions that can be invoked on objects
|
| 52960 |
jmc |
88 |
from this class. These can also be created by invoking the
|
|
|
89 |
\code{$methods} method on the generator object returned. %$
|
|
|
90 |
See the section on \dQuote{Writing Reference Methods} for details.
|
| 53068 |
jmc |
91 |
|
|
|
92 |
Two optional method names are interpreted specially, \code{initialize}
|
|
|
93 |
and \code{finalize}. If an \code{initialize} method is defined, it
|
|
|
94 |
will be invoked when an object is generated from the class. See the
|
|
|
95 |
discussion of method \code{$new(...)} %$
|
|
|
96 |
in the section \dQuote{Reference Object Generators}.
|
|
|
97 |
|
|
|
98 |
If a \code{finalize} method is defined, a function will be
|
|
|
99 |
\link[=reg.finalizer]{registered} to invoke it before the environment
|
|
|
100 |
in the object is discarded by the garbage collector. See the matrix
|
| 56985 |
maechler |
101 |
viewer example for both initialize and finalize methods.}
|
| 53068 |
jmc |
102 |
|
| 52816 |
jmc |
103 |
\item{where}{
|
| 61034 |
jmc |
104 |
the environment in which to store the class definition (or to begin the
|
|
|
105 |
search for it, in
|
|
|
106 |
the case of \code{getRefClass()}). Defaults to
|
| 52816 |
jmc |
107 |
the package namespace or environment for code that is part of an \R{}
|
|
|
108 |
package, and to the global environment for code sourced directly at
|
|
|
109 |
the session top level.
|
|
|
110 |
}
|
|
|
111 |
\item{\dots}{
|
|
|
112 |
other arguments to be passed to \code{\link{setClass}}.
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
| 61034 |
jmc |
116 |
\value{
|
|
|
117 |
\code{setRefClass()} returns a generator function suitable for
|
|
|
118 |
creating objects from the class, invisibly. A call to this function
|
|
|
119 |
takes any number of arguments,
|
|
|
120 |
which will be passed on to the initialize method. If no
|
|
|
121 |
\code{initialize} method is defined for the class or one of its
|
|
|
122 |
superclasses, the default method expects named arguments with the
|
|
|
123 |
name of one of the fields and unnamed arguments, if any, that are
|
|
|
124 |
objects from one of the superclasses of this class (but only
|
|
|
125 |
superclasses that are themselves reference classes have any effect).
|
|
|
126 |
|
|
|
127 |
The generator function is similar to the S4 generator function
|
|
|
128 |
returned by \code{\link{setClass}}. In addition to being a generator
|
|
|
129 |
function, however, it is also a reference class generator object,
|
|
|
130 |
with reference class methods for various utilities. See the section
|
|
|
131 |
on reference class generator objects below.
|
|
|
132 |
|
|
|
133 |
If the class has a method defined for \code{$initialize()}, %$
|
|
|
134 |
this method will be called once the reference object has been
|
|
|
135 |
created. You should write such a method for a class that needs to do
|
|
|
136 |
some special initialization.
|
|
|
137 |
In particular, a reference method is recommended rather than a method
|
|
|
138 |
for the S4 generic function \code{initialize()}, because some special initialization is
|
|
|
139 |
required for reference objects \emph{before} the initialization of
|
|
|
140 |
fields.
|
|
|
141 |
As with S4 classes, methods are written for \code{$initialize()} %$
|
|
|
142 |
and not for \code{$new()}, %$
|
|
|
143 |
both for the previous reason and also because \code{$new()} %$
|
|
|
144 |
is invoked on the generator object and would be a method for that class.
|
|
|
145 |
|
|
|
146 |
The default method for \code{$initialize()} %$
|
|
|
147 |
is equivalent to invoking the method \code{$initFields(...)}. %$
|
|
|
148 |
Named arguments assign initial values to the corresponding fields.
|
|
|
149 |
Unnamed arguments must be objects from this class or a reference
|
|
|
150 |
superclass of this class.
|
|
|
151 |
Fields will be initialized to the contents of the fields in such
|
|
|
152 |
objects, but named arguments override the corresponding inherited
|
|
|
153 |
fields.
|
|
|
154 |
Note that fields are simply assigned. If the field is itself a
|
|
|
155 |
reference object, that object is not copied.
|
|
|
156 |
The new and previous object will share the reference.
|
|
|
157 |
Also, a field assigned from an unnamed argument counts as an
|
|
|
158 |
assignment for locked fields.
|
|
|
159 |
To override an inherited value for a locked field, the new value must
|
|
|
160 |
be one of the named arguments in the initializing call.
|
|
|
161 |
A later assignment of the field will result in an error.
|
|
|
162 |
|
|
|
163 |
For technical reasons, the
|
|
|
164 |
default method does not currently appear explicitly, but can be
|
|
|
165 |
invoked by \code{$callSuper(...)} %$
|
|
|
166 |
from a method for \code{$initialize()}. %$
|
|
|
167 |
Initialization methods need some care in design, as they do for S4
|
|
|
168 |
classes.
|
|
|
169 |
In particular, remember that others may subclass your class and pass
|
|
|
170 |
through field assignments or other arguments. Therefore, your method
|
|
|
171 |
should normally include \dots as an argument, all other arguments
|
|
|
172 |
should have defaults or check for missingness, and your method should
|
|
|
173 |
pass all initialized values on via \code{$callSuper()} or \code{$initFields()} if
|
|
|
174 |
you know that your superclasses have no initialization methods.
|
|
|
175 |
|
|
|
176 |
\code{getRefClass()} also returns the generator function for the
|
|
|
177 |
class. Note that the package slot in the value is the correct package
|
|
|
178 |
from the class definition, regardless of the \code{where} argument,
|
|
|
179 |
which is used only to
|
|
|
180 |
find the class if necessary.
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
|
| 52816 |
jmc |
184 |
\section{Reference Objects}{
|
| 52903 |
jmc |
185 |
Normal objects in \R are passed as arguments in function calls consistently with
|
| 52816 |
jmc |
186 |
functional programming semantics; that is, changes made to an object
|
|
|
187 |
passed as an argument are local to the function call. The object that
|
|
|
188 |
supplied the argument is unchanged.
|
|
|
189 |
|
| 62255 |
jmc |
190 |
The functional model (sometimes called pass-by-value, although this is
|
|
|
191 |
inaccurate for \R) is
|
| 52903 |
jmc |
192 |
suitable for many statistical computations and is implicit, for
|
| 52816 |
jmc |
193 |
example, in the basic \R software for fitting statistical models.
|
| 52903 |
jmc |
194 |
In some other situations, one would like all the code dealing with an
|
| 52816 |
jmc |
195 |
object to see the exact same content, so that changes made in any
|
| 52903 |
jmc |
196 |
computation would be reflected everywhere.
|
| 52816 |
jmc |
197 |
This is often suitable if the object has some \dQuote{objective}
|
|
|
198 |
reality, such as a window in a user interface.
|
|
|
199 |
|
| 52903 |
jmc |
200 |
In addition, commonly used languages, including Java, C++ and many
|
| 52816 |
jmc |
201 |
others, support a version of classes and methods assuming reference
|
|
|
202 |
semantics.
|
| 53068 |
jmc |
203 |
The corresponding programming mechanism
|
|
|
204 |
is to invoke a method on an object.
|
| 62255 |
jmc |
205 |
In the \R syntax we use \code{"$"} %$
|
| 59654 |
jmc |
206 |
for this operation; one invokes a method,
|
| 52816 |
jmc |
207 |
\code{m1} say, on an object \code{x} by the expression
|
| 52903 |
jmc |
208 |
\code{x$m1(...)}. %$
|
| 53068 |
jmc |
209 |
|
|
|
210 |
Methods in this paradigm are associated with the object, or more
|
| 53003 |
jmc |
211 |
precisely with the class of the object, as opposed to methods in a
|
|
|
212 |
function-based class/method system, which are fundamentally associated
|
|
|
213 |
with the function (in \R, for example, a generic function in an \R
|
|
|
214 |
session has a table of all its currently known methods).
|
|
|
215 |
In this document \dQuote{methods for a class} as opposed to
|
|
|
216 |
\dQuote{methods for a function} will make the distinction.
|
|
|
217 |
|
| 53068 |
jmc |
218 |
Objects in this paradigm usually have named fields on which
|
| 52903 |
jmc |
219 |
the methods operate.
|
| 53003 |
jmc |
220 |
In the \R implementation, the fields are defined when the class is
|
|
|
221 |
created.
|
|
|
222 |
The field itself can optionally have a specified class, meaning that only objects
|
|
|
223 |
from this class or one of its subclasses can be assigned to the field.
|
|
|
224 |
By default, fields have class \code{"ANY"}.
|
|
|
225 |
Fields may also be defined by supplying an accessor function which
|
|
|
226 |
will be called to get or set the field.
|
|
|
227 |
Accessor functions are likely when reference classes are part of an
|
|
|
228 |
inter-system interface.
|
|
|
229 |
The interface will usually supply the accessor functions automatically
|
|
|
230 |
based on the definition of the corresponding class in the other language.
|
|
|
231 |
|
| 53068 |
jmc |
232 |
Fields are accessed by reference.
|
| 53003 |
jmc |
233 |
In particular, invoking a method may modify the content of
|
| 52903 |
jmc |
234 |
the fields.
|
| 52816 |
jmc |
235 |
|
| 53003 |
jmc |
236 |
Programming for such classes involves writing new methods for a
|
|
|
237 |
particular class.
|
| 53068 |
jmc |
238 |
In the \R implementation, these methods are \R functions, with zero or
|
|
|
239 |
more formal arguments.
|
| 53003 |
jmc |
240 |
The object on which the methods are invoked is not an explicit
|
|
|
241 |
argument to the method.
|
|
|
242 |
Instead, fields and methods for the class can be referred to by name
|
|
|
243 |
in the method definition.
|
|
|
244 |
The implementation uses \R environments to make fields and methods
|
|
|
245 |
available by name.
|
| 53068 |
jmc |
246 |
Additional special fields allow reference to the complete object and
|
| 62255 |
jmc |
247 |
to the definition of the class. See the section on \dQuote{Writing
|
|
|
248 |
Reference Methods}.
|
| 53003 |
jmc |
249 |
|
| 52816 |
jmc |
250 |
The goal of the software described here is to provide a uniform
|
| 53068 |
jmc |
251 |
programming style in \R for software dealing with reference classes, whether
|
|
|
252 |
implemented directly in \R or through an interface to one of the OOP
|
| 52816 |
jmc |
253 |
languages.
|
| 53003 |
jmc |
254 |
}
|
| 52816 |
jmc |
255 |
|
| 52903 |
jmc |
256 |
|
|
|
257 |
\section{Writing Reference Methods}{
|
| 53003 |
jmc |
258 |
Reference methods are functions supplied as elements of a named list,
|
|
|
259 |
either
|
|
|
260 |
when invoking \code{g$methods()} %$}
|
|
|
261 |
on a generator object \code{g} or as
|
|
|
262 |
the argument \code{methods} in a call to \code{setRefClass}.
|
| 52816 |
jmc |
263 |
They are written as ordinary \R functions but have some special
|
|
|
264 |
features and restrictions.
|
| 52903 |
jmc |
265 |
The body of the function can contain calls to any other reference method,
|
| 53003 |
jmc |
266 |
including those inherited from other reference classes and may refer
|
|
|
267 |
to fields in the object by name.
|
| 52816 |
jmc |
268 |
|
| 53003 |
jmc |
269 |
Fields may be modified in a method by using the
|
|
|
270 |
non-local assignment operator, \code{<<-}, as in the \code{$edit} and \code{$undo}
|
|
|
271 |
methods in the example below.
|
|
|
272 |
Note that non-local assignment is required: a local assignment with
|
| 52816 |
jmc |
273 |
the \code{<-} operator just creates a local object in the function
|
|
|
274 |
call, as it would in any \R function.
|
| 53275 |
jmc |
275 |
When methods are installed, a heuristic check is made for local
|
|
|
276 |
assignments to field names and a warning issued if any are detected.
|
| 52816 |
jmc |
277 |
|
| 52903 |
jmc |
278 |
Reference methods should be kept simple; if they need to do some
|
|
|
279 |
specialized \R computation, that computation should use a separate \R
|
|
|
280 |
function that is called from the reference method.
|
| 52846 |
jmc |
281 |
Specifically, methods can not use special features of the
|
| 53003 |
jmc |
282 |
enclosing environment mechanism, since the method's environment is
|
|
|
283 |
used to access fields and other methods.
|
| 58004 |
jmc |
284 |
In particular, methods should not use non-exported entries in the
|
|
|
285 |
package's namespace, because the methods may be inherited by a
|
|
|
286 |
reference class in another package.
|
|
|
287 |
|
| 52903 |
jmc |
288 |
Reference methods can not themselves be generic functions; if you want
|
| 52856 |
jmc |
289 |
additional function-based method dispatch, write a separate generic
|
| 52846 |
jmc |
290 |
function and call that from the method.
|
| 52816 |
jmc |
291 |
|
| 52846 |
jmc |
292 |
The entire object can be referred to in a method by the reserved
|
|
|
293 |
name \code{.self}, as shown in the \code{save=} method of the
|
| 52816 |
jmc |
294 |
example.
|
| 53003 |
jmc |
295 |
The special object \code{.refClassDef} contains the definition of the
|
|
|
296 |
class of the object.
|
| 55779 |
jmc |
297 |
These fields are read-only (it makes no sense to modify these
|
|
|
298 |
references), with one exception.
|
| 62255 |
jmc |
299 |
In principal, the \code{.self} field can be modified in the \code{$initialize} %$
|
| 55779 |
jmc |
300 |
method, because the object is still being created at this stage.
|
| 62255 |
jmc |
301 |
This is definitely not recommended, unless to set some
|
|
|
302 |
non-reference properties of the object defined for this class, which
|
|
|
303 |
is itself not recommended if it mixes slots and fields.
|
| 52816 |
jmc |
304 |
|
| 62255 |
jmc |
305 |
|
| 53003 |
jmc |
306 |
The methods available include methods inherited from superclasses, as
|
| 52846 |
jmc |
307 |
discussed in the next section.
|
| 53003 |
jmc |
308 |
|
| 59654 |
jmc |
309 |
Only methods actually used will be included in the environment
|
| 62255 |
jmc |
310 |
corresponding to an individual object. To declare that a method requires a
|
|
|
311 |
particular other method, the first method should include a call
|
| 59654 |
jmc |
312 |
to \code{$usingMethods()} %$
|
|
|
313 |
with the name of the other method as an argument.
|
|
|
314 |
Declaring the methods this way is essential if the other method is used indirectly (e.g., via \code{\link{sapply}()}
|
|
|
315 |
or \code{\link{do.call}()}).
|
|
|
316 |
If it is called directly, code analysis will find it.
|
|
|
317 |
Declaring the method is harmless in any case, however, and may aid
|
|
|
318 |
readability of the source code.
|
|
|
319 |
|
| 53003 |
jmc |
320 |
Documentation for the methods can be obtained by the \code{$help} %$}
|
|
|
321 |
method for the generator object.
|
| 53068 |
jmc |
322 |
Methods for classes are not documented in the \code{Rd} format used
|
|
|
323 |
for \R functions.
|
|
|
324 |
Instead, the \code{$help} %$}
|
|
|
325 |
method prints the calling sequence of the method, followed by
|
|
|
326 |
self-documentation from the method definition, in the style of Python.
|
| 53003 |
jmc |
327 |
If the first element of the body of the method is a literal character
|
| 53068 |
jmc |
328 |
string (possibly multi-line), that string is interpreted as documentation.
|
|
|
329 |
See the method definitions in the example.
|
| 52816 |
jmc |
330 |
}
|
|
|
331 |
\section{Inheritance}{
|
|
|
332 |
Reference classes inherit from other reference classes by using the
|
|
|
333 |
standard \R inheritance; that is, by including the superclasses in the
|
|
|
334 |
\code{contains=} argument when creating the new class.
|
|
|
335 |
The names of the reference superclasses are in slot
|
|
|
336 |
\code{refSuperClasses} of the class definition.
|
| 62255 |
jmc |
337 |
Reference classes can inherit from ordinary S4 classes also, but this
|
|
|
338 |
is usually a bad idea if it mixes reference fields and non-reference slots.
|
|
|
339 |
See the comments in the section on \dQuote{Implementation}.
|
| 52816 |
jmc |
340 |
|
| 53003 |
jmc |
341 |
Class fields are inherited. A class definition can override a field
|
|
|
342 |
of the same name in a superclass only if the overriding class is a
|
|
|
343 |
subclass of the class of the inherited field. This ensures that a
|
|
|
344 |
valid object in the field remains valid for the superclass as well.
|
| 52816 |
jmc |
345 |
|
| 52846 |
jmc |
346 |
Inherited methods are installed in the same way as directly
|
| 52856 |
jmc |
347 |
specified methods.
|
| 52846 |
jmc |
348 |
The code in a method can refer to inherited methods in the same
|
|
|
349 |
way as directly specified methods.
|
|
|
350 |
|
| 53003 |
jmc |
351 |
A method may override a method of the same name in a superclass.
|
| 56985 |
maechler |
352 |
The overriding method can call the superclass method by
|
| 53068 |
jmc |
353 |
\code{callSuper(...)} as described below.
|
|
|
354 |
|
| 54337 |
jmc |
355 |
All reference classes inherit from the class \code{"envRefClass"},
|
|
|
356 |
which provides the following methods.
|
| 53068 |
jmc |
357 |
|
|
|
358 |
\describe{
|
|
|
359 |
\item{\code{$callSuper(...)}}{ %$
|
|
|
360 |
Calls the method inherited from a reference superclass.
|
|
|
361 |
The call is meaningful only from within another method, and will be
|
|
|
362 |
resolved to call the inherited method of the same name.
|
| 53003 |
jmc |
363 |
The arguments to \code{$callSuper} %$}
|
|
|
364 |
are passed to the superclass version.
|
| 53068 |
jmc |
365 |
See the matrix viewer class in the example.
|
|
|
366 |
|
| 53003 |
jmc |
367 |
Note that the intended arguments for the superclass method must be
|
|
|
368 |
supplied explicitly; there is no convention for supplying the
|
|
|
369 |
arguments automatically, in contrast to the similar mechanism for
|
|
|
370 |
functional methods.
|
| 53068 |
jmc |
371 |
}
|
| 52816 |
jmc |
372 |
|
| 53669 |
jmc |
373 |
\item{\code{$copy(shallow = FALSE)}}{ %$
|
|
|
374 |
Creates a copy of the object. With reference classes, unlike ordinary
|
|
|
375 |
\R objects, merely assigning the object with a different name does not
|
|
|
376 |
create an independent copy. If \code{shallow} is \code{FALSE}, any
|
|
|
377 |
field that is itself a reference object will also be copied, and
|
|
|
378 |
similarly recursively for its fields. Otherwise, while reassigning a
|
|
|
379 |
field to a new reference object will have no side effect, modifying
|
|
|
380 |
such a field will still be reflected in both copies of the object.
|
|
|
381 |
The argument has no effect on non-reference objects in fields. When
|
|
|
382 |
there are reference objects in some fields but it is asserted that
|
|
|
383 |
they will not be modified, using \code{shallow = TRUE} will save some
|
|
|
384 |
memory and time.
|
|
|
385 |
}
|
|
|
386 |
|
| 53696 |
jmc |
387 |
\item{\code{$field(name, value)}}{ %$
|
|
|
388 |
With one argument, returns the field of the object with character
|
|
|
389 |
string \code{name}. With two arguments, the corresponding field is
|
|
|
390 |
assigned \code{value}. Assignment checks that \code{name} specifies a
|
|
|
391 |
valid field, but the single-argument version will attempt to get
|
|
|
392 |
anything of that name from the object's environment.
|
|
|
393 |
|
|
|
394 |
The \code{$field()} %$
|
|
|
395 |
method replaces the direct use of a field name, when the name of the
|
|
|
396 |
field must be calculated, or for looping over several fields.
|
|
|
397 |
}
|
|
|
398 |
|
| 56335 |
jmc |
399 |
\item{\code{$export(Class)}}{ %$
|
|
|
400 |
Returns the result of coercing the object to \code{Class} (typically
|
|
|
401 |
one of the superclasses of the object's class). Calling the method
|
|
|
402 |
has no side effect on the object itself.
|
|
|
403 |
}
|
|
|
404 |
|
| 53669 |
jmc |
405 |
\item{\code{$getRefClass()}; \code{$getClass()}}{
|
|
|
406 |
These return respectively the generator object and the formal class
|
|
|
407 |
definition for the reference class of this object, efficiently.
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
|
|
|
411 |
|
| 53068 |
jmc |
412 |
\item{\code{$import(value, Class = class(value))}}{ %$
|
|
|
413 |
Import the object \code{value} into the current object, replacing the
|
|
|
414 |
corresponding fields in the current object.
|
|
|
415 |
Object \code{value} must come from one of the superclasses of the
|
|
|
416 |
current object's class.
|
|
|
417 |
If argument \code{Class} is supplied, \code{value} is first coerced to
|
|
|
418 |
that class.
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
\item{\code{$initFields(...)}}{ %$
|
|
|
422 |
Initialize the fields of the object from the supplied arguments. This
|
|
|
423 |
method is usually only called from a class with a \code{$initialize()}% $
|
|
|
424 |
method. It corresponds to the default initialization for reference
|
|
|
425 |
classes. If there are slots and non-reference superclasses, these may
|
|
|
426 |
be supplied in the \dots argument as well.
|
|
|
427 |
|
|
|
428 |
Typically, a specialized \code{$initialize()}% $
|
|
|
429 |
method carries out its own computations, then invokes \code{$initFields()}% $
|
|
|
430 |
to perform standard initialization, as shown in the
|
|
|
431 |
\code{matrixViewer} class in the example below.
|
|
|
432 |
}
|
| 55578 |
jmc |
433 |
|
| 56335 |
jmc |
434 |
\item{\code{$show()}}{ %$
|
|
|
435 |
This method is called when the object is printed automatically,
|
|
|
436 |
analogously to the \code{\link{show}} function. A general method is
|
|
|
437 |
defined for class \code{"envRefClass"}. User-defined reference
|
|
|
438 |
classes will often define their own method: see the Example below.
|
|
|
439 |
|
|
|
440 |
Note two points in the example. As with any \code{show()} method, it
|
|
|
441 |
is a good idea to print the class explicitly to allow for subclasses
|
|
|
442 |
using the method. Second, to call the \emph{function} \code{show()}
|
|
|
443 |
from the method, as opposed to the \code{$show()} %$
|
|
|
444 |
method itself, refer to \code{methods::show()} explicitly.
|
|
|
445 |
}
|
|
|
446 |
|
| 56985 |
maechler |
447 |
\item{\code{$trace(what, ...)}, \code{$untrace(what)} }{
|
| 55578 |
jmc |
448 |
Apply the tracing and debugging facilities of the \code{\link{trace}}
|
|
|
449 |
function to the reference method \code{what}.
|
|
|
450 |
|
|
|
451 |
All the arguments of the \code{\link{trace}}
|
|
|
452 |
function can be supplied, except for \code{signature}, which is not
|
|
|
453 |
meaningful.
|
|
|
454 |
|
|
|
455 |
The reference method can be invoked on either an object or the
|
|
|
456 |
generator for the class. See the section on Debugging below for details.
|
|
|
457 |
}
|
| 59654 |
jmc |
458 |
|
|
|
459 |
\item{\code{$usingMethods(...)}}{ %$
|
|
|
460 |
Reference methods used by this method are named as the arguments
|
|
|
461 |
either quoted or unquoted. In the code analysis phase of installing the
|
| 59660 |
hornik |
462 |
the present method, the declared methods will be included. It is essential
|
| 59654 |
jmc |
463 |
to declare any methods used in a nonstandard way (e.g., via an apply function).
|
|
|
464 |
Methods called directly do not need to be declared, but it is harmless to do so.
|
|
|
465 |
\code{$usingMethods()} does nothing at run time. %$
|
|
|
466 |
}
|
| 53068 |
jmc |
467 |
} % end describe
|
|
|
468 |
|
|
|
469 |
Objects also inherit two reserved fields:
|
|
|
470 |
\describe{
|
|
|
471 |
\item{\code{.self}}{
|
|
|
472 |
a reference to the entire object;
|
|
|
473 |
}
|
|
|
474 |
\item{\code{.refClassDef}}{
|
|
|
475 |
the class definition.
|
|
|
476 |
}
|
|
|
477 |
} % end \describe
|
|
|
478 |
The defined fields should not override these, and in general it is
|
|
|
479 |
unwise to define a field whose name begins with \code{"."}, since the
|
|
|
480 |
implementation may use such names for special purposes.
|
|
|
481 |
|
|
|
482 |
}
|
|
|
483 |
|
| 61034 |
jmc |
484 |
\section{Reference Class Generators}{
|
| 53003 |
jmc |
485 |
The call to \code{setRefClass} defines the specified class and
|
| 61034 |
jmc |
486 |
returns a \dQuote{generator function} object for that class.
|
| 61058 |
jmc |
487 |
This object has class \code{"refObjectGenerator"}; it inherits
|
| 61034 |
jmc |
488 |
from \code{"function"} via \code{"classGeneratorFunction"} and can be
|
|
|
489 |
called to generate new objects from the reference class.
|
|
|
490 |
|
|
|
491 |
The returned object is also a reference class object, although not of
|
|
|
492 |
the standard construction.
|
|
|
493 |
It can be used to invoke reference methods and access fields in the usual way, but
|
|
|
494 |
instead of being implemented directly as an environment it has a
|
|
|
495 |
subsidiary generator object as a slot, a
|
|
|
496 |
standard reference object (of class
|
| 61058 |
jmc |
497 |
\code{"refGeneratorSlot"}).
|
| 61034 |
jmc |
498 |
Note that if one wanted to extend the reference class generator
|
|
|
499 |
capability with a subclass, this should be done by subclassing
|
| 61058 |
jmc |
500 |
\code{"refGeneratorSlot"}, not \code{"refObjectGenerator"}.
|
| 61034 |
jmc |
501 |
|
|
|
502 |
The fields are \code{def}, the class definition, and \code{className},
|
| 53003 |
jmc |
503 |
the character string name of the class.
|
| 61034 |
jmc |
504 |
Methods generate objects
|
| 53003 |
jmc |
505 |
from the class, to access help on reference methods, and to
|
|
|
506 |
define new reference methods for the class.
|
|
|
507 |
The currently available methods are:
|
| 52903 |
jmc |
508 |
\describe{
|
|
|
509 |
\item{\code{$new(...)}}{ %$
|
| 61034 |
jmc |
510 |
This method is equivalent to calling the generator function returned
|
|
|
511 |
by \code{setRefClass}.
|
| 52903 |
jmc |
512 |
}
|
| 53470 |
jmc |
513 |
|
| 52903 |
jmc |
514 |
\item{\code{$help(topic)}}{ %$
|
|
|
515 |
Prints brief help on the topic. The topics recognized
|
|
|
516 |
are reference method names, quoted or not.
|
|
|
517 |
|
|
|
518 |
The information printed is the calling sequence for the method, plus
|
|
|
519 |
self-documentation if any.
|
|
|
520 |
Reference methods can have an initial character string or vector as
|
|
|
521 |
the first element in the body of the function defining the method.
|
|
|
522 |
If so, this string is taken as self-documentation for the method (see
|
| 53068 |
jmc |
523 |
the section on \dQuote{Writing Reference Methods} for details).
|
| 52903 |
jmc |
524 |
|
|
|
525 |
If no topic is given or if the topic is not a method name, the
|
|
|
526 |
definition of the class is printed.
|
|
|
527 |
}
|
|
|
528 |
\item{\code{$methods(...)}}{ %$
|
| 53003 |
jmc |
529 |
With no arguments, returns a list of the reference methods for this
|
|
|
530 |
class.
|
|
|
531 |
|
|
|
532 |
Named arguments
|
|
|
533 |
are method definitions, which will be
|
| 52903 |
jmc |
534 |
installed in the class, as if they had been supplied in the
|
| 53003 |
jmc |
535 |
\code{methods} argument to \code{setRefClass()}.
|
| 55988 |
jmc |
536 |
Supplying methods in this way, rather than in the call to
|
|
|
537 |
\code{setRefClass()}, is largely for the sake of clearer source code
|
|
|
538 |
when many or large methods are being defined.
|
|
|
539 |
All methods for a class should be defined in the source code that
|
|
|
540 |
defines the class, typically as part of a package.
|
|
|
541 |
In particular, methods can not be redefined in a class in an attached
|
|
|
542 |
package with a namespace: The class method checks for a locked
|
|
|
543 |
binding of the class definition.
|
| 52903 |
jmc |
544 |
|
| 55988 |
jmc |
545 |
|
| 52903 |
jmc |
546 |
The new methods can refer to any currently defined method by name
|
|
|
547 |
(including other methods supplied in this call to
|
|
|
548 |
\code{$methods()}. %$
|
|
|
549 |
Note though that previously defined methods are not re-analyzed
|
|
|
550 |
meaning that they will not call the new method (unless it redefines an
|
|
|
551 |
existing method of the same name).
|
|
|
552 |
|
|
|
553 |
To remove a method, supply \code{NULL} as its new definition.
|
|
|
554 |
}
|
| 53003 |
jmc |
555 |
|
|
|
556 |
\item{\code{$fields()}}{ %$}{
|
|
|
557 |
Returns a list of the fields, each with its corresponding class.
|
|
|
558 |
Fields for which an accessor function was supplied in the definition
|
|
|
559 |
have class \code{"activeBindingFunction"}.
|
| 52903 |
jmc |
560 |
}
|
| 53003 |
jmc |
561 |
|
|
|
562 |
\item{\code{$lock(...)}}{ %$}{
|
|
|
563 |
The fields named in the arguments are locked; specifically, after the
|
|
|
564 |
lock method is called, the field may be set once. Any further attempt
|
|
|
565 |
to set it will generate an error.
|
|
|
566 |
|
| 57589 |
jmc |
567 |
If called with no arguments, the method returns the names of the
|
|
|
568 |
locked fields.
|
|
|
569 |
|
| 53003 |
jmc |
570 |
Fields that are defined by an explicit accessor function can not be
|
|
|
571 |
locked (on the other hand, the accessor function can be defined to
|
|
|
572 |
generate an error if called with an argument).
|
| 55988 |
jmc |
573 |
|
|
|
574 |
All code to lock fields should normally be part of the definition of a
|
|
|
575 |
class; that is, the read-only nature of the fields is meant to be part
|
|
|
576 |
of the class definition, not a dynamic property added later.
|
|
|
577 |
In particular, fields can not be locked in a class in an attached
|
|
|
578 |
package with a namespace: The class method checks for a locked
|
| 57589 |
jmc |
579 |
binding of the class definition. Locked fields can not be
|
|
|
580 |
subsequently unlocked.
|
| 52903 |
jmc |
581 |
}
|
|
|
582 |
|
| 55578 |
jmc |
583 |
\item{\code{$trace(what, ..., classMethod = FALSE)}}{ %$}{
|
|
|
584 |
Establish a traced version of method \code{what} for objects generated
|
|
|
585 |
from this class. The generator object tracing works like the
|
|
|
586 |
\code{$trace()}%$
|
|
|
587 |
method for objects from the class, with two differences.
|
|
|
588 |
Since it changes the method definition in the class object itself,
|
|
|
589 |
tracing applies to all objects, not just the one on which the trace
|
|
|
590 |
method is invoked.
|
|
|
591 |
|
|
|
592 |
Second, the optional argument \code{classMethod = TRUE} allows tracing
|
| 61034 |
jmc |
593 |
on the methods of the generator object itself.
|
| 55578 |
jmc |
594 |
By default, \code{what} is interpreted as the name of a method in the
|
|
|
595 |
class for which this object is the generator.
|
|
|
596 |
}
|
|
|
597 |
|
| 53003 |
jmc |
598 |
\item{\code{$accessors(...)}}{ %$}{
|
|
|
599 |
A number of
|
|
|
600 |
systems using the OOP programming paradigm recommend or enforce
|
|
|
601 |
\emph{getter and setter methods}
|
|
|
602 |
corresponding to each field, rather than direct access by name.
|
| 62255 |
jmc |
603 |
If you like this style and want to extract a field named \code{abc}
|
|
|
604 |
by \code{x$getAbc()} and assign it by
|
|
|
605 |
\code{x$setAbc(value)},
|
|
|
606 |
the \code{$accessors} %$}
|
|
|
607 |
method is a convenience function that creates such getter and setter methods for the
|
| 53003 |
jmc |
608 |
specified fields.
|
| 62255 |
jmc |
609 |
Otherwise there is no reason to use this mechanism. In particular, it
|
|
|
610 |
has nothing to do with the general ability to define fields by
|
|
|
611 |
functions as described in the section on \dQuote{Reference Objects}.
|
| 53003 |
jmc |
612 |
}
|
|
|
613 |
} %% end of \describe
|
|
|
614 |
} %% end of \section
|
|
|
615 |
|
|
|
616 |
\section{Implementation}{
|
|
|
617 |
Reference classes are implemented as S4 classes with a data part of
|
|
|
618 |
type \code{"environment"}.
|
|
|
619 |
Fields correspond to named objects in the environment.
|
| 62255 |
jmc |
620 |
A field associated with a function is implemented as an
|
| 53003 |
jmc |
621 |
\link[=bindenv]{active binding}.
|
| 62255 |
jmc |
622 |
In particular, fields with a specified class are implemented as a
|
| 53003 |
jmc |
623 |
special form of active binding to enforce valid assignment to the
|
|
|
624 |
field.
|
|
|
625 |
A field, say \code{data}, can be accessed generally by an expression
|
|
|
626 |
of the form \code{x$data} %$}
|
|
|
627 |
for any object from the relevant class.
|
|
|
628 |
In a method for this class, the field can be accessed by the name
|
|
|
629 |
\code{data}.
|
|
|
630 |
A field that is not locked can be set by an expression of the form
|
|
|
631 |
\code{x$data <- value}.%$
|
|
|
632 |
Inside a method, a field can be assigned by an expresion of the form
|
|
|
633 |
\code{x <<- value}.
|
|
|
634 |
Note the \link[=assignOps]{non-local assignment} operator.
|
|
|
635 |
The standard \R interpretation of this operator works to assign it in
|
|
|
636 |
the environment of the object.
|
| 53068 |
jmc |
637 |
If the field has an accessor function defined, getting and setting
|
|
|
638 |
will call that function.
|
| 53003 |
jmc |
639 |
|
| 53068 |
jmc |
640 |
When a method is invoked on an object, the function defining the method is
|
|
|
641 |
installed in the object's environment, with the same environment as the
|
| 53003 |
jmc |
642 |
environment of the function.
|
| 53068 |
jmc |
643 |
|
| 62255 |
jmc |
644 |
Because of the implementation, new reference classes can inherit from
|
|
|
645 |
non-reference S4 classes as well as reference classes.
|
|
|
646 |
This is usually a bad idea, if the slots from the non-reference
|
|
|
647 |
class are thought of as alternatives to fields.
|
|
|
648 |
Unless there is some special argument in favor, mixing the functional
|
|
|
649 |
and reference paradigms for properties of the same object is
|
|
|
650 |
conceptually unclear.
|
|
|
651 |
In addition, the initialization method for the class will have to sort
|
|
|
652 |
out fields from slots, with a good chance of creating anomalous
|
|
|
653 |
behavior for subclasses of this class.
|
|
|
654 |
Better in general to define fields analogous to the slots in the S4
|
|
|
655 |
class, and to initialize those from an S4 object of that class.
|
|
|
656 |
|
| 53003 |
jmc |
657 |
}
|
|
|
658 |
|
| 52856 |
jmc |
659 |
\section{Inter-System Interfaces}{
|
| 56985 |
maechler |
660 |
A number of
|
| 53068 |
jmc |
661 |
languages use a similar reference-based programming model with classes
|
|
|
662 |
and class-based methods.
|
| 52856 |
jmc |
663 |
Aside from differences in choice of terminology and other details,
|
| 53068 |
jmc |
664 |
many of these languages are compatible with the programming style
|
| 52856 |
jmc |
665 |
described here.
|
| 53068 |
jmc |
666 |
\R interfaces to the languages exist in a number of packages.
|
| 52856 |
jmc |
667 |
|
| 53068 |
jmc |
668 |
The reference class definitions here provide a hook for
|
| 54337 |
jmc |
669 |
classes in the foreign language to be exposed in \R.
|
|
|
670 |
Access to fields and/or methods in the class can be
|
| 53068 |
jmc |
671 |
implemented by defining an \R reference class corresponding to
|
|
|
672 |
classes made available through the interface.
|
| 53003 |
jmc |
673 |
Typically, the inter-system interface will take care of the details of
|
| 54337 |
jmc |
674 |
creating the \R class, given a description of the foreign class (what fields
|
| 53003 |
jmc |
675 |
and methods it has, the classes for the fields, whether any are
|
|
|
676 |
read-only, etc.)
|
|
|
677 |
The specifics for the fields and methods can be implemented via
|
|
|
678 |
reference methods for the \R class.
|
|
|
679 |
In particular, the use of active bindings allows field access for
|
| 53068 |
jmc |
680 |
getting and setting, with
|
| 53003 |
jmc |
681 |
actual access handled by the inter-system interface.
|
| 52903 |
jmc |
682 |
|
| 53003 |
jmc |
683 |
\R methods and/or fields can be included in the class definition as for any
|
|
|
684 |
reference class.
|
| 54337 |
jmc |
685 |
The methods can use or set fields and can call other methods transparently
|
|
|
686 |
whether the field or method comes from the interface or is defined
|
|
|
687 |
directly in \R.
|
| 52903 |
jmc |
688 |
|
| 53003 |
jmc |
689 |
For an inter-system interface using this approach, see the code for package \code{Rcpp}, version
|
|
|
690 |
0.8.7 or later.
|
| 52856 |
jmc |
691 |
}
|
| 61034 |
jmc |
692 |
% \value{
|
|
|
693 |
% \code{setRefClass()} and \code{getRefClass()} both return a generator object for the class. This is
|
|
|
694 |
% itself a reference object, with methods to generate objects from the
|
|
|
695 |
% class and also for defining new methods and for help-style
|
|
|
696 |
% documentation. See the
|
|
|
697 |
% section on \dQuote{Reference Class Generator Objects} for details.
|
|
|
698 |
% Note that \code{Class} in the call to \code{getRefClass()} can be an
|
|
|
699 |
% object from the corresponding class, and that a similar reference
|
|
|
700 |
% class method \code{$getRefClass()} %$
|
|
|
701 |
% is available as well.
|
| 52816 |
jmc |
702 |
|
| 61034 |
jmc |
703 |
% \code{setRefClass} defines the class and stores its class definition.
|
|
|
704 |
% \code{getRefClass} requires that the class has been defined as a
|
|
|
705 |
% reference class.
|
| 52816 |
jmc |
706 |
|
| 61034 |
jmc |
707 |
% }
|
| 55578 |
jmc |
708 |
\section{Debugging}{
|
|
|
709 |
The standard \R{} debugging and tracing facilities can be applied to
|
|
|
710 |
reference methods.
|
|
|
711 |
Reference methods can be passed to \code{\link{debug}} and its
|
|
|
712 |
relatives from an object to debug further method invocations on that
|
|
|
713 |
object; for example, \code{debug(xx$edit)}. %$
|
|
|
714 |
|
|
|
715 |
Somewhat more flexible use is available for a reference method version
|
|
|
716 |
of the \code{\link{trace}} function.
|
|
|
717 |
A corresponding \code{$trace()} %$
|
|
|
718 |
reference method is available for
|
|
|
719 |
either an object or for the reference class generator
|
| 56335 |
jmc |
720 |
(\code{xx$trace()} or \code{mEdit$trace()} in the example below).
|
| 55578 |
jmc |
721 |
Using \code{$trace()} on an object sets up a tracing
|
|
|
722 |
version for future invocations of the specified method for that
|
|
|
723 |
object.
|
|
|
724 |
Using \code{$trace()} on the generator for the class sets up a
|
| 62255 |
jmc |
725 |
tracing version for all future objects from that class (and sometimes for
|
|
|
726 |
existing objects from the class if the method is not declared or
|
|
|
727 |
previously invoked).
|
| 55578 |
jmc |
728 |
|
|
|
729 |
In either case, all the arguments to the standard \code{\link{trace}}
|
|
|
730 |
function are available, except for \code{signature=} which is
|
|
|
731 |
meaningless since reference methods can not be S4 generic functions.
|
|
|
732 |
This includes the typical style \code{trace(what, browser)} for
|
|
|
733 |
interactive debugging and \code{trace(what, edit = TRUE)} to edit the
|
|
|
734 |
reference method interactively.
|
|
|
735 |
|
| 52903 |
jmc |
736 |
}
|
| 52816 |
jmc |
737 |
\author{
|
| 56985 |
maechler |
738 |
John Chambers
|
| 52816 |
jmc |
739 |
}
|
|
|
740 |
|
| 56985 |
maechler |
741 |
\examples{
|
| 53003 |
jmc |
742 |
## a simple editor for matrix objects. Method $edit() changes some
|
|
|
743 |
## range of values; method $undo() undoes the last edit.
|
| 56335 |
jmc |
744 |
mEdit <- setRefClass("mEdit",
|
| 52960 |
jmc |
745 |
fields = list( data = "matrix",
|
| 52816 |
jmc |
746 |
edits = "list"),
|
| 52960 |
jmc |
747 |
methods = list(
|
| 52816 |
jmc |
748 |
edit = function(i, j, value) {
|
| 53003 |
jmc |
749 |
## the following string documents the edit method
|
|
|
750 |
'Replaces the range [i, j] of the
|
|
|
751 |
object by value.
|
|
|
752 |
'
|
| 52816 |
jmc |
753 |
backup <-
|
|
|
754 |
list(i, j, data[i,j])
|
| 53003 |
jmc |
755 |
data[i,j] <<- value
|
| 54337 |
jmc |
756 |
edits <<- c(edits, list(backup))
|
| 52816 |
jmc |
757 |
invisible(value)
|
|
|
758 |
},
|
|
|
759 |
undo = function() {
|
| 53003 |
jmc |
760 |
'Undoes the last edit() operation
|
|
|
761 |
and update the edits field accordingly.
|
|
|
762 |
'
|
| 52960 |
jmc |
763 |
prev <- edits
|
| 54337 |
jmc |
764 |
if(length(prev)) prev <- prev[[length(prev)]]
|
| 52816 |
jmc |
765 |
else stop("No more edits to undo")
|
|
|
766 |
edit(prev[[1]], prev[[2]], prev[[3]])
|
|
|
767 |
## trim the edits list
|
|
|
768 |
length(edits) <<- length(edits) - 2
|
|
|
769 |
invisible(prev)
|
| 56335 |
jmc |
770 |
},
|
|
|
771 |
show = function() {
|
|
|
772 |
'Method for automatically printing matrix editors'
|
|
|
773 |
cat("Reference matrix editor object of class",
|
|
|
774 |
classLabel(class(.self)), "\n")
|
|
|
775 |
cat("Data: \n")
|
|
|
776 |
methods::show(data)
|
|
|
777 |
cat("Undo list is of length", length(edits), "\n")
|
| 52816 |
jmc |
778 |
}
|
| 52960 |
jmc |
779 |
))
|
| 56985 |
maechler |
780 |
|
| 52960 |
jmc |
781 |
xMat <- matrix(1:12,4,3)
|
| 61034 |
jmc |
782 |
xx <- mEdit(data = xMat)
|
| 52960 |
jmc |
783 |
xx$edit(2, 2, 0)
|
| 56335 |
jmc |
784 |
xx
|
| 52960 |
jmc |
785 |
xx$undo()
|
| 56335 |
jmc |
786 |
mEdit$help("undo")
|
| 52960 |
jmc |
787 |
stopifnot(all.equal(xx$data, xMat))
|
| 53003 |
jmc |
788 |
|
| 56985 |
maechler |
789 |
utils::str(xx) # show fields and names of non-trivial methods
|
|
|
790 |
|
| 53003 |
jmc |
791 |
## add a method to save the object
|
| 56335 |
jmc |
792 |
mEdit$methods(
|
| 53003 |
jmc |
793 |
save = function(file) {
|
|
|
794 |
'Save the current object on the file
|
|
|
795 |
in R external object format.
|
|
|
796 |
'
|
|
|
797 |
base::save(.self, file = file)
|
|
|
798 |
}
|
| 56985 |
maechler |
799 |
)
|
| 53003 |
jmc |
800 |
|
| 52960 |
jmc |
801 |
tf <- tempfile()
|
| 56335 |
jmc |
802 |
xx$save(tf)
|
| 53003 |
jmc |
803 |
\dontshow{
|
| 52960 |
jmc |
804 |
load(tf)
|
|
|
805 |
unlink(tf)
|
|
|
806 |
stopifnot(identical(xx$data, .self$data))
|
|
|
807 |
}
|
| 52816 |
jmc |
808 |
|
| 53068 |
jmc |
809 |
\dontrun{
|
| 52816 |
jmc |
810 |
## Inheriting a reference class: a matrix viewer
|
| 56985 |
maechler |
811 |
mv <- setRefClass("matrixViewer",
|
| 53068 |
jmc |
812 |
fields = c("viewerDevice", "viewerFile"),
|
| 56335 |
jmc |
813 |
contains = "mEdit",
|
| 53068 |
jmc |
814 |
methods = list( view = function() {
|
|
|
815 |
dd <- dev.cur(); dev.set(viewerDevice)
|
|
|
816 |
devAskNewPage(FALSE)
|
|
|
817 |
matplot(data, main = paste("After",length(edits),"edits"))
|
|
|
818 |
dev.set(dd)},
|
| 52816 |
jmc |
819 |
edit = # invoke previous method, then replot
|
|
|
820 |
function(i, j, value) {
|
| 52903 |
jmc |
821 |
callSuper(i, j, value)
|
| 52816 |
jmc |
822 |
view()
|
|
|
823 |
}))
|
|
|
824 |
|
| 53068 |
jmc |
825 |
## initialize and finalize methods
|
| 56985 |
maechler |
826 |
mv$methods( initialize =
|
| 53470 |
jmc |
827 |
function(file = "./matrixView.pdf", ...) {
|
|
|
828 |
viewerFile <<- file
|
| 53068 |
jmc |
829 |
pdf(viewerFile)
|
|
|
830 |
viewerDevice <<- dev.cur()
|
|
|
831 |
dev.set(dev.prev())
|
| 53470 |
jmc |
832 |
callSuper(...)
|
| 53068 |
jmc |
833 |
},
|
|
|
834 |
finalize = function() {
|
|
|
835 |
dev.off(viewerDevice)
|
|
|
836 |
})
|
| 55578 |
jmc |
837 |
|
|
|
838 |
## debugging an object: call browser() in method $edit()
|
|
|
839 |
xx$trace(edit, browser)
|
|
|
840 |
|
| 56335 |
jmc |
841 |
## debugging all objects from class mEdit in method $undo()
|
|
|
842 |
mEdit$trace(undo, browser)
|
| 53068 |
jmc |
843 |
}
|
| 52816 |
jmc |
844 |
\dontshow{
|
| 56335 |
jmc |
845 |
removeClass("mEdit")
|
| 53068 |
jmc |
846 |
resetGeneric("$")
|
|
|
847 |
resetGeneric("initialize")
|
|
|
848 |
} %$
|
| 52816 |
jmc |
849 |
}
|
|
|
850 |
\keyword{ programming }
|
|
|
851 |
\keyword{ classes }
|