r - 在 R 中保存引用类的实例

标签 r oop reference-class

我正在尝试重复 ReferenceClasses 帮助页面末尾的示例 在 R 文档中:

## 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(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)
     }
)

x = matrix(1:12, 3, 4)
xx = mEdit(data=x)
xx$show()
xx$edit(2, 2, 0)
xx$show()
xx$undo()
xx$show()
mEdit$help("undo")
xx$data == x
utils::str(xx)
tf = paste(tempfile(), ".Rdata", sep="")
tf
xx$save(tf)

一切正常,除了保存的对象似乎不包含任何内容:

/////////////////////////////////
load("fileb473ff584c9.Rdata")
/////////////////////////////////
ls()
character(0)

最佳答案

将保存方法的定义改为

mEdit$methods(
    save = function(file) {
        'Save the current object on the file
        in R external object format.'
        myMat = .self
        base::save(myMat, file = file)
    }
)

现在,当您发出 ls 命令时,myMat 将被列出

或将 ls 与 all.names = TRUE 一起使用,因为变量 .self 默认隐藏

> ls(all.names = TRUE)
[1] ".__C__Config"           ".__C__foo"              ".__C__mEdit"            ".__global__"           
[5] ".Random.seed"           ".rChart_object"         ".requireCachedGenerics" ".self"                 
> .self
Reference matrix editor object of class "mEdit" 
Data: 
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
Undo list is of length 0 

在 mEdit 类上启用制表符补全

.DollarNames.mEdit <- function(x, pattern){
  grep(pattern, getRefClass(class(x))$methods(), value=TRUE)
}

关于r - 在 R 中保存引用类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079751/

相关文章:

r - 使用 expression() 显示多个不等号

r - 我有一个单词数据框,我想过滤掉 R 中单词列中有数字的行

r - 如何用大数据集绘制树状图?

java - 如何根据传递的类型调用枚举方法?

r - 为什么 R 在引用类对象上使用属性时反复无常?

r - 函数参数列表中的函数 (x) 在 R 中意味着什么?

php - 调用未定义的方法 dbConnection::query() - 如何修复它?

oop - 单例模式 - Head First Design Patterns 书中的疑问

r - 引用类的并行计算

引用类字段消失