I have an S3 object in R, something like:
myObject <- list(
someParameter1 = 4,
someList = 1:10
)
class(myObject) <- "myClass"
I created an extraction method for this class:
"[.myClass" <- function(x, i) {
x$someList[i] * x$someParameter1
}
myObject[5]
# 20
Now I want to create an assignment method (from ?Extract
I understand that's called a subassignment), so that I can write:
myObject[5] <- 250
myObject[5]
# 1000
I first naively tried to write this as
"[<-.myClass" <- function(x, i, value) {
x$someList[i] <- value
}
but for some reason this replaces myObject
with value
. I suspect I must modify x
and then assign("someName", x, pos=somewhere)
, but how can I reliably determine someName
and somewhere
?
Or is there an different way to do this?