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

本文标签: bioconductorRDefine S4 setter properly for setting each element separately in a named listStack Overflow