【问题标题】:Loading variable into object from an RData file, is it memory-effecient?将变量从 RData 文件加载到对象中,是否节省内存?
【发布时间】:2014-06-15 15:02:37
【问题描述】:

这是一个例子:

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 的值?如果它们的尺寸很大,会不会有问题? 有没有更好的方法从文件中加载变量?

【问题讨论】:

    标签: r reference-class


    【解决方案1】:

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

    load(filename, envir=globalenv())
    

    【讨论】:

    • 如果我想加载到 MyClass 对象中怎么办?
    猜你喜欢
    • 1970-01-01
    • 2014-02-12
    • 2020-08-06
    • 2013-07-13
    • 1970-01-01
    • 2013-03-19
    • 2013-04-03
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多