我发现自己需要更新之前使用save创建的Rdata文件中的一个或两个数据对象。如果我不小心加载文件,我可能会忘记重新保存文件中的一些对象。例如,我正在处理一个包,其中的一些对象存储在sysdata.rda (供内部使用的查找表,我不想导出)中,并且只想考虑更新单个对象。
我还没有弄清楚是否有一种标准的方法来做到这一点,所以我创建了自己的函数。
resave <- function (..., list = character(), file = stop("'file' must be specified")) {
# create a staging environment to load the existing R objects
stage <- new.env()
load(file, envir=stage)
# get the list of objects to be "resaved"
names <- as.character(substitute(list(...)))[-1L]
list <- c(list, names)
# copy the objects to the staging environment
lapply(list, function(obj) assign(obj, get(obj), stage))
# save everything in the staging environment
save(list=ls(stage, all.names=TRUE), file=file)
}不过,这看起来有点过头了。有没有更好/更简单的方法来做到这一点?
顺便说一句,我假设在函数的作用域中创建的新环境在函数调用后被销毁,这是正确的吗?
发布于 2012-08-05 09:51:55
以下是一个略短的版本:
resave <- function(..., list = character(), file) {
previous <- load(file)
var.names <- c(list, as.character(substitute(list(...)))[-1L])
for (var in var.names) assign(var, get(var, envir = parent.frame()))
save(list = unique(c(previous, var.names)), file = file)
}我利用了load函数返回已加载变量的名称这一事实,因此我可以使用该函数的环境,而不是创建一个。在使用get时,我非常小心地只查看调用函数的环境,即parent.frame()。
这是一个模拟:
x1 <- 1
x2 <- 2
x3 <- 3
save(x1, x2, x3, file = "abc.RData")
x1 <- 10
x2 <- 20
x3 <- 30
resave(x1, x3, file = "abc.RData")
load("abc.RData")
x1
# [1] 10
x2
# [1] 2
x3
# [1] 30发布于 2015-11-02 01:48:08
我已经在stackoverflow包中添加了@flodel答案的重构版本。它明确地使用环境是为了更具防御性。
resave <- function(..., list = character(), file) {
e <- new.env()
load(file, e)
list <- union(list, as.character(substitute((...)))[-1L])
copyEnv(parent.frame(), e, list)
save(list = ls(e, all.names=TRUE), envir = e, file = file)
}https://stackoverflow.com/questions/11813096
复制相似问题