0

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?

qed
  • 22,298
  • 21
  • 125
  • 196

1 Answers1

0

The load() command allows you to specify which environment to load the data into. If you want to load into the global environment, just do that directly with

load(filename, envir=globalenv())
MrFlick
  • 195,160
  • 17
  • 277
  • 295