Search code examples
roopr-s4

writing names(object) <- assignment method in R


I'm writing methods for an S4 class. I have written a "names" method as it follows:

setMethod("names","markovchain", 
       function(x) {
        out <- x@states
        return(out)
      }
)

thus returning the slot x@states composing the S4 markovchain object. Now I would like to add the names method the capability to set the x@states values. How can I do it? Thanks in advance.


Solution

  • Define a names<- method:

    setMethod("names<-","markovchain", 
       function(x,value) {
        x@states<-value
        return(x)
       }
    )