R 引用类继承中的Missage参数
以下是官方帮助页面中的一个示例:R 引用类继承中的Missage参数,r,oop,inheritance,reference-class,R,Oop,Inheritance,Reference Class,以下是官方帮助页面中的一个示例: ## 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",
## 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)
})
数据不在。。。阿格?为什么R说它丢失了?您可以为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会导致通过callSuper调用来自mEdit的initialize方法
因此,继承按预期工作。为它指定一个默认值initialize=functiondata=matrixThanks。刚刚检查了文档:因此,您的方法通常应该包括。。。作为一个参数,所有其他参数都应该有默认值或检查是否丢失,如果您知道您的超类没有初始化方法,那么您的方法应该通过$callSuper或$initFields传递所有初始化值。您能给出一个答案并对此进行解释吗?我不确定这里的文档是什么意思。根据帮助页面,我应该添加。。。甚至到mEdit$initialize,并调用initFields。。。在里面?
Loading testRefClass
Error in .Object$initialize(...) (from testRefClass.R#52) :
argument "data" is missing, with no default
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
> rr <- mv()
> rr
Reference matrix editor object of class "matrixViewer"
Data:
[,1]
[1,] NA
Undo list is of length 0
> 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