Maybe I am thinking of R classes as if they were classes in C or Java, but I cannot seem to modify values:
test <- function() {
inc <- function() {
x <- attr( obj, "x" )
x <- x + 1
print(x)
attr( obj, "x" ) <- x
return( obj )
}
obj <- list(inc=inc)
attr( obj, "x" ) <- 1
class(obj) <- c('test')
return( obj )
}
When I run this:
> t <- test()
> t <- t$inc()
[1] 2
> t <- t$inc()
[1] 2
It is as if the original class object cannot be modified.