0

这是一个例子:

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

在初始化函数中,是否复制了 x 和 y 的值?如果它们的尺寸很大,会不会有问题?有没有更好的方法从文件中加载变量?

4

1 回答 1

0

load()命令允许您指定将数据加载到哪个环境中。如果要加载到全局环境中,只需直接使用

load(filename, envir=globalenv())
于 2014-06-15T16:49:53.777 回答