Here is an example:
MyClass = setRefClass("MyClass", fields = c("x", "y"))
MyClass$methods(
initialize = function(xinit=0, yinit=0, filename="") {
if(file.exists(filename)) {
message("yes, it is there!")
load(filename)
print(x)
print(y)
x <<- x
y <<- y
} else {
x <<- xinit
y <<- yinit
}
}
)
x = 15
y = 20
save(x, y, file="/tmp/xydata")
## z = MyClass(x, y)
z = MyClass(filename="/tmp/xydata")
z$x
z$y
In the initialize function, are the values of x and y copied? If their size is large, would it be a problem? Is there a better way to load variables from a file?