gpt4 book ai didi

r - 为 S4 引用类的实例定义默认字段值

转载 作者:行者123 更新时间:2023-12-04 13:53:37 28 4
gpt4 key购买 nike

如何定义字段的默认值 S4 引用类 实例?

对于普通的 S4 类(class),有 prototype争论:

setClass("Test_1",
representation(
x.1="numeric",
x.2="logical",
x.3="matrix"
),
prototype=list(
x.1=10,
x.2=FALSE,
x.3=matrix(0,0,0)
)
)

> new("Test_1")
An object of class "Test_1"
Slot "x.1":
[1] 10

Slot "x.2":
[1] FALSE

Slot "x.3":
<0 x 0 matrix>

据我了解 setRefClass的帮助页面,这也应该适用于 S4 引用类,通过 ...争论。相应的部分说:

... other arguments to be passed to setClass.



然而 prototype似乎没有发送到 setClass正确:
gen <- setRefClass("Test_2",
fields=list(
x.1="numeric",
x.2="logical",
x.3="matrix"
),
prototype=list(
x.1=10,
x.2=FALSE,
x.3=matrix(0,0,0)
)
)

> gen$new()
Reference class object of class "Test_2"
Field "x.1":
numeric(0)
Field "x.2":
logical(0)
Field "x.3":
<0 x 0 matrix>

或者
> new("Test_2")
Reference class object of class "Test_2"
Field "x.1":
numeric(0)
Field "x.2":
logical(0)
Field "x.3":
<0 x 0 matrix>

我在 setRefClass 的帮助页面上没有找到与默认值/原型(prototype)相关的任何其他内容.

这是一个错误还是我在这里遗漏了一些明显的东西?

编辑

我能找到的最接近的可以帮助我声明默认值的是 $initFields() .

这就是 ?setRefClass不得不说:

Initialize the fields of the object from the supplied arguments. This method is usually only called from a class with a $initialize() method. It corresponds to the default initialization for reference classes. If there are slots and non-reference superclasses, these may be supplied in the ... argument as well.

Typically, a specialized $initialize() method carries out its own computations, then invokes $initFields() to perform standard initialization, as shown in the matrixViewer class in the example below.



到现在为止还挺好
gen <- setRefClass("Test_3",
fields=list(
x.1="numeric",
x.2="logical",
x.3="matrix"
),
methods=list(
initialize=function(
...
) {
.self$initFields(x.1=10, x.2=TRUE, x.3=matrix(0,0,0), ...)
}
)
)

适用于“默认初始化案例”:
> gen$new()
Reference class object of class "Test_3"
Field "x.1":
[1] 10
Field "x.2":
[1] TRUE
Field "x.3":
<0 x 0 matrix>

但是,如果无法处理在初始化时明确指定(某些)字段值的情况:
> gen$new(x.1=100)
Reference class object of class "Test_3"
Field "x.1":
[1] 10
Field "x.2":
[1] TRUE
Field "x.3":
<0 x 0 matrix>

解决方法

真的很脏,但它的工作原理
gen <- setRefClass("Test_4",
fields=list(
x.1="numeric",
x.2="logical",
x.3="matrix"
),
methods=list(
initialize=function(
...
) {
defaults <- list(
x.1=10,
x.2=FALSE,
x.3=matrix(0,0,0)
)
if (!missing(...)) {
x.args <- list(...)
specified <- names(x.args)
idx <- which(specified %in% names(defaults))
if (length(idx)) {
for (ii in specified[idx]) {
defaults[[ii]] <- x.args[[ii]]
}
}
}
.self$initFields(x.1=defaults$x.1, x.2=defaults$x.2,
x.3=defaults$x.3, ...)
}
)
)

初始化
> gen$new()
Reference class object of class "Test_4"
Field "x.1":
[1] 10
Field "x.2":
[1] FALSE
Field "x.3":
<0 x 0 matrix>

> gen$new(x.1=100)
Reference class object of class "Test_4"
Field "x.1":
[1] 100
Field "x.2":
[1] FALSE
Field "x.3":
<0 x 0 matrix>
> gen$new(x.1=100)

这就是我正在寻找的东西,但我确定还有更多“内置”的东西?

编辑 2

整个事情有点通用。方法 ensureDefaultValues可以是每个其他类继承自的类的方法。对于“继承路径更远”的类,可以简单地在 intialize 中调用此方法。方法:
gen <- setRefClass("RootClass",
methods=list(
ensureDefaultValues=function(values, ...) {
if (!missing(...)) {
arguments <- list(...)
specified <- names(arguments)
idx <- which(specified %in% names(values))
if (length(idx)) {
for (ii in specified[idx]) {
values[[ii]] <- arguments[[ii]]
}
}
}
temp <- paste(paste0(names(values), "=values$",
names(values)), collapse=", ")
eval(parse(text=paste0(".self$initFields(", temp, ", ...)")))
return(TRUE)
}
)
)

gen <- setRefClass("Test_5",
contains="RootClass",
fields=list(
x.1="numeric",
x.2="logical",
x.3="matrix"
),
methods=list(
initialize=function(
...
) {
.self$ensureDefaultValues(
values=list(
x.1=10,
x.2=FALSE,
x.3=matrix(0,0,0)
),
...
)
return(.self)
}
)
)
> gen$new()
Reference class object of class "Test_5"
Field "x.1":
[1] 10
Field "x.2":
[1] FALSE
Field "x.3":
<0 x 0 matrix>

> gen$new(x.1=100)
Reference class object of class "Test_5"
Field "x.1":
[1] 100
Field "x.2":
[1] FALSE
Field "x.3":
<0 x 0 matrix>

最佳答案

我不认为原型(prototype)的概念旨在为引用类实现。一种简单的方法是在 initialize 的参数中提供默认值。 , 作为

Test_1 <- setRefClass("Test_1",
field = list(x.1="numeric", x.2="logical", x.3="matrix"),
method = list(initialize =
function(..., x.1 = 10, x.2 = FALSE, x.3 = matrix(0, 0, 0))
{
callSuper(..., x.1 = x.1, x.2 = x.2, x.3 = x.3)
})
)

然后可以调用生成器函数(使用 R-devel 中可用的语法)返回
> Test_1()
Reference class object of class "Test_1"
Field "x.1":
[1] 10
Field "x.2":
[1] FALSE
Field "x.3":
<0 x 0 matrix>

关于r - 为 S4 引用类的实例定义默认字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13517007/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com