admin管理员组文章数量:1122846
So I have my object, let's said a<- new ("A")
I'm calling a@param$stuff <- "MyStuff"
to set the value. I know it's not a good practice.
It's better to use a setter.
I'm using the following code to set a specific attribute of slot param
in my Class A
. param
is a named list as you can see.
I do
param(a)$stuff <- "MyStuff"
I think I'm not doing it well because in fact it's using the accessor, I may be wrong...
The following manner I provide next is ok I think when you set the whole list but i was wondering to do it by element.
param(a) <- list(stuff="MyStuff")
setClass("A",
slots = c(
param = "list"
),
prototype = list(
param = list(stuff = FALSE)
)
)
# ACCESSOR
setGeneric("param", signature="x",
function(x) standardGeneric("param")
)
setMethod("param", "A", function(x) x@param)
# SETTER
setGeneric("param<-", signature=c("x", "value"),
function(x, value) standardGeneric("param<-")
)
setMethod("param<-", "A", function(x, value) {
x@param <- value
methods::validObject(x)
x
})
版权声明:本文标题:bioconductor - R : Define S4 setter properly for setting each element separately in a named list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304196a1932149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论