I have found myself in the position of needing to update one or two data objects in an Rdata file previously created using save. If I'm not careful to load the file I can forget to re-save some objects in the file. As an example, I'm working on a package with some objects stored in sysdata.rda (look-up tables for internal use which I do not want to export) and only want to worry about updating individual objects.
I haven't managed to work out if there is a standard way to do this, so created my own function.
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)
}
It does seem like overkill though. Is there a better/easier way to do this?
As an aside, am I right in assuming that a new environment created in the scope of a function is destroyed after the function call?
Here is a slightly shorter version:
I took advantage of the fact the
loadfunction returns the name of the loaded variables, so I could use the function's environment instead of creating one. And when usingget, I was careful to only look in the environment from which the function is called, i.e.parent.frame().Here is a simulation: