The R Project SVN R

Rev

Rev 52985 | Rev 53212 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 52985 Rev 53068
Line 139... Line 139...
139
stopifnot(identical(ab1$a, 1), identical(ab1$b, 1))
139
stopifnot(identical(ab1$a, 1), identical(ab1$b, 1))
140
 
140
 
141
ab1$b <- 2
141
ab1$b <- 2
142
 
142
 
143
stopifnot(identical(ab1$a, 2), identical(ab1$b, 2))
143
stopifnot(identical(ab1$a, 2), identical(ab1$b, 2))
-
 
144
 
-
 
145
## a simple editor for matrix objects.  Method  $edit() changes some
-
 
146
## range of values; method $undo() undoes the last edit.
-
 
147
mEditor <- setRefClass("matrixEditor",
-
 
148
      fields = list( data = "matrix",
-
 
149
        edits = "list"),
-
 
150
      methods = list(
-
 
151
     edit = function(i, j, value) {
-
 
152
       ## the following string documents the edit method
-
 
153
       'Replaces the range [i, j] of the
-
 
154
        object by value.
-
 
155
        '
-
 
156
         backup <-
-
 
157
             list(i, j, data[i,j])
-
 
158
         data[i,j] <<- value
-
 
159
         edits <<- c(list(backup),
-
 
160
                     edits)
-
 
161
         invisible(value)
-
 
162
     },
-
 
163
     undo = function() {
-
 
164
       'Undoes the last edit() operation
-
 
165
        and update the edits field accordingly.
-
 
166
        '
-
 
167
         prev <- edits
-
 
168
         if(length(prev)) prev <- prev[[1]]
-
 
169
         else stop("No more edits to undo")
-
 
170
         edit(prev[[1]], prev[[2]], prev[[3]])
-
 
171
         ## trim the edits list
-
 
172
         length(edits) <<- length(edits) - 2
-
 
173
         invisible(prev)
-
 
174
     }
-
 
175
     ))
-
 
176
xMat <- matrix(1:12,4,3)
-
 
177
xx <- mEditor$new(data = xMat)
-
 
178
xx$edit(2, 2, 0)
-
 
179
xx$data
-
 
180
xx$undo()
-
 
181
mEditor$help("undo")
-
 
182
stopifnot(all.equal(xx$data, xMat))
-
 
183
 
-
 
184
## add a method to save the object
-
 
185
mEditor$methods(
-
 
186
     save = function(file) {
-
 
187
       'Save the current object on the file
-
 
188
        in R external object format.
-
 
189
       '
-
 
190
         base::save(.self, file = file)
-
 
191
     }
-
 
192
)
-
 
193
 
-
 
194
tf <- tempfile()
-
 
195
xx$save(tf) #$
-
 
196
load(tf)
-
 
197
unlink(tf)
-
 
198
stopifnot(identical(xx$data, .self$data))
-
 
199
 
-
 
200
markViewer <- ""
-
 
201
setMarkViewer <- function(what)
-
 
202
    markViewer <<- what
-
 
203
 
-
 
204
## Inheriting a reference class:  a matrix viewer
-
 
205
mv <- setRefClass("matrixViewer",
-
 
206
    fields = c("viewerDevice", "viewerFile"),
-
 
207
    contains = "matrixEditor",
-
 
208
    methods = list( view = function() {
-
 
209
        dd <- dev.cur(); dev.set(viewerDevice)
-
 
210
        devAskNewPage(FALSE)
-
 
211
        matplot(data, main = paste("After",length(edits),"edits"))
-
 
212
        dev.set(dd)},
-
 
213
        edit = # invoke previous method, then replot
-
 
214
          function(i, j, value) {
-
 
215
            callSuper(i, j, value)
-
 
216
            view()
-
 
217
          }))
-
 
218
 
-
 
219
## initialize and finalize methods
-
 
220
mv$methods( initialize = function(...) {
-
 
221
    viewerFile <<- tempfile("matrixView")
-
 
222
    pdf(viewerFile)
-
 
223
    viewerDevice <<- dev.cur()
-
 
224
    message("Plotting to ", viewerFile)
-
 
225
    dev.set(dev.prev())
-
 
226
    setMarkViewer("ON")
-
 
227
    initFields(...)
-
 
228
  },
-
 
229
  finalize = function() {
-
 
230
    dev.off(viewerDevice)
-
 
231
    setMarkViewer("OFF")
-
 
232
  })
-
 
233
 
-
 
234
ff = new("matrixViewer", data = xMat)
-
 
235
stopifnot(identical(markViewer, "ON")) # check initialize
-
 
236
ff$edit(2,2,0)
-
 
237
ff$data
-
 
238
ff$undo()
-
 
239
stopifnot(all.equal(ff$data, xMat))
-
 
240
rm(ff)
-
 
241
gc()
-
 
242
stopifnot(identical(markViewer, "OFF")) #check finalize