I have an object ob
of class markovchain
.
> ob
An object of class "markovchain"
Slot "states":
[1] "a" "b" "c" "d" "e"
Slot "byrow":
[1] TRUE
Slot "transitionMatrix":
a b c d e
a 1 2 3 4 5
b 6 7 8 9 10
c 11 12 13 14 15
d 16 17 18 19 20
e 21 22 23 24 25
Slot "name":
[1] "deepak"
My task is to create a method to get and set the data of name
slot.
Below is the method to get the data in the name
slot.
> setGeneric("name", function(object) standardGeneric("name"))
[1] "name"
> setMethod("name", "markovchain",
+ function(object) {
+ out <- object@name
+ return(out)
+ }
+ )
[1] "name"
It is working properly. See
> name(ob)
[1] "deepak"
Now my task is to set the data in the name
slot. I have tried this
setGeneric("name<-", function(object, ob_name) standardGeneric("name<-"))
setMethod("name<-", "markovchain",
function(object, ob_name) {
object@name <- ob_name
object
}
)
While setting the name I am getting an error.
> name(ob) <- "apple"
Error in `name<-`(`*tmp*`, value = "apple") :
unused argument (value = "apple")
I am not getting what I am doing wrong? Any help.
The problem is solved. Name of the last argument of the function must be value
.
setGeneric("name", function(object) standardGeneric("name"))
setMethod("name", "markovchain", function(object) {
out <- object@name
return(out)
})
setGeneric("name<-", function(object, value) standardGeneric("name<-"))
setMethod("name<-", "markovchain",
function(object, value) {
object@name <- value
object
}
)
See the output.
> name(ob)
[1] "deepak"
> name(ob) <- "value is changed"
> name(ob)
[1] "value is changed"