【问题标题】:Missage argument in inheritance of reference class引用类继承中的错误参数
【发布时间】:2014-08-14 20:46:32
【问题描述】:

以下是官方帮助页面的示例:

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


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



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
mv$methods( initialize =
                function(file = "./matrixView.pdf", ...) {
                    viewerFile <<- file
                    pdf(viewerFile)
                    viewerDevice <<- dev.cur()
                    dev.set(dev.prev())
                    callSuper(...)
                },
            finalize = function() {
                dev.off(viewerDevice)
            })

一切都像宣传的那样工作,但如果我添加这样的东西:

mEdit$methods(
    initialize = function(data) {
        data <<- data
    }
)

然后尝试加载类,R 报错:

Loading testRefClass
Error in .Object$initialize(...) (from testRefClass.R#52) : 
  argument "data" is missing, with no default

data 不是在 ... 参数中吗?为什么 R 说它不见了?

【问题讨论】:

  • 给它一个默认的initialize = function(data = matrix())
  • 谢谢。刚刚检查了文档: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.
  • 你能发表一个答案并解释一下吗?我不确定文档在这里的含义。

标签: r oop inheritance reference-class


【解决方案1】:

可以给initialize方法中传递的参数默认值:

mEdit$methods(
  initialize = function(data = matrix()) {
    data <<- data
  }
)

 nn <- mEdit()
> nn
Reference matrix editor object of class "mEdit" 
Data: 
     [,1]
[1,]   NA
Undo list is of length 0 

> nn <- mEdit(data = matrix(1:4, 2,2))
> nn
Reference matrix editor object of class "mEdit" 
Data: 
     [,1] [,2]
[1,]    1    3
[2,]    2    4
Undo list is of length 0 

这应该会处理错误消息。

mv 继承自 mEdit,因此应在其 initialize 方法中调用 callSuper(...)

> rr <- mv()
> rr
Reference matrix editor object of class "matrixViewer" 
Data: 
     [,1]
[1,]   NA
Undo list is of length 0 

我们可以看到,实例化一个新的mv 会导致mEdit 中的initialize 方法被callSuper(...) 调用。

> rr <- mv(data = matrix(1:4, 2,2))
> rr
Reference matrix editor object of class "matrixViewer" 
Data: 
     [,1] [,2]
[1,]    1    3
[2,]    2    4
Undo list is of length 0 

所以继承按预期工作。

【讨论】:

  • 根据帮助页面,我应该在mEdit$initialize()中添加...,并在其中调用initFields(...)?
猜你喜欢
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
相关资源
最近更新 更多