| 61562 |
jmc |
1 |
% File src/library/methods/man/refClass.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 64662 |
ripley |
3 |
% Copyright 2010-2013 R Core Team
|
| 61562 |
jmc |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
|
|
6 |
\name{LocalReferenceClasses}
|
|
|
7 |
\title{Localized Objects based on Reference Classes}
|
|
|
8 |
\alias{LocalReferenceClasses}
|
|
|
9 |
\alias{localRefClass-class}
|
|
|
10 |
\alias{$<-,localRefClass-method} %$
|
|
|
11 |
\description{
|
|
|
12 |
Local reference classes are modified \link{ReferenceClasses} that
|
|
|
13 |
isolate the objects to the local frame. Therefore, they do \emph{not}
|
|
|
14 |
propagate changes back to the calling environment. At the same time,
|
|
|
15 |
they use the reference field semantics locally, avoiding the automatic
|
|
|
16 |
duplication applied to standard \R objects.
|
|
|
17 |
|
|
|
18 |
The current implementation has no special construction. To create a
|
|
|
19 |
local reference class, call \code{\link{setRefClass}()} with a
|
|
|
20 |
\code{contains=} argument that includes \code{"localRefClass"}. See
|
|
|
21 |
the example below.
|
|
|
22 |
|
|
|
23 |
Local reference classes operate essentially as do regular, functional
|
|
|
24 |
classes in \R; that is, changes are made by assignment and take place
|
|
|
25 |
in the local frame.
|
|
|
26 |
The essential difference is that replacement operations (like the
|
|
|
27 |
change to the \code{twiddle} field in the example) do not cause
|
|
|
28 |
duplication of the entire object, as would be the case for a formal
|
|
|
29 |
class or for data with attributes or in a named list.
|
|
|
30 |
The purpose is to allow large objects in some fields that are not
|
|
|
31 |
changed along with potentially frequent changes to other fields, but
|
|
|
32 |
without copying the large fields.
|
|
|
33 |
|
|
|
34 |
}
|
|
|
35 |
\usage{
|
| 62209 |
hornik |
36 |
\special{setRefClass(Class, fields = , contains = c("localRefClass",....),
|
|
|
37 |
methods =, where =, ...)}
|
| 61562 |
jmc |
38 |
}
|
|
|
39 |
|
|
|
40 |
\details{
|
|
|
41 |
Localization of objects is only partially automated in the current implementation.
|
|
|
42 |
Replacement expressions using the \code{$<-} operator are safe. %$
|
|
|
43 |
|
|
|
44 |
However, if reference methods for the class themselves modify fields,
|
|
|
45 |
using \code{<<-}, for example, then
|
|
|
46 |
one must ensure that the object is local to the relevant frame before
|
|
|
47 |
any such method is called.
|
|
|
48 |
Otherwise, standard reference class behavior still prevails.
|
|
|
49 |
|
|
|
50 |
There are two ways to ensure locality. The direct way is to invoke
|
|
|
51 |
the special
|
|
|
52 |
method \code{x$ensureLocal()} on the object.
|
|
|
53 |
The other way is to modify a field explicitly by \code{x$field <- ...}
|
|
|
54 |
It's
|
|
|
55 |
only necessary that one or the other of these happens
|
|
|
56 |
once for each object, in order to trigger the shallow copy that
|
|
|
57 |
provides locality for the references. In the example below, we show
|
|
|
58 |
both mechanisms.
|
|
|
59 |
|
|
|
60 |
However it's done, localization must occur
|
|
|
61 |
\emph{before} any methods make changes. (Eventually, some use of code
|
|
|
62 |
tools should at least largely automate this process, although it may
|
|
|
63 |
be difficult to guarantee success under arbitrary circumstances.)
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
\author{
|
|
|
67 |
John Chambers
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
\examples{
|
|
|
71 |
## class "myIter" has a BigData field for the real (big) data
|
|
|
72 |
## and a "twiddle" field for some parameters that it twiddles
|
|
|
73 |
## ( for some reason)
|
|
|
74 |
|
|
|
75 |
myIter <- setRefClass("myIter", contains = "localRefClass",
|
|
|
76 |
fields = list(BigData = "numeric", twiddle = "numeric"))
|
|
|
77 |
|
|
|
78 |
tw <- rnorm(3)
|
|
|
79 |
x1 <- myIter(BigData = rnorm(1000), twiddle = tw) # OK, not REALLY big
|
|
|
80 |
|
|
|
81 |
twiddler <- function(x, n) {
|
|
|
82 |
x$ensureLocal() # see the Details. Not really needed in this example
|
|
|
83 |
for(i in seq(length = n)) {
|
|
|
84 |
x$twiddle <- x$twiddle + rnorm(length(x$twiddle))
|
|
|
85 |
## then do something ....
|
|
|
86 |
## Snooping in gdb, etc will show that x$BigData is not copied
|
|
|
87 |
}
|
|
|
88 |
return(x)
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
x2 <- twiddler(x1, 10)
|
|
|
92 |
|
|
|
93 |
stopifnot(identical(x1$twiddle, tw), !identical(x1$twiddle, x2$twiddle))
|
|
|
94 |
|
|
|
95 |
}
|
|
|
96 |
\keyword{ programming }
|
|
|
97 |
\keyword{ classes }
|