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?
save
doesn't have an "append" option. I'll ask r-help and get back here if there's a useful answer. – Bowlessave
function as an "append =T" option, but it's not a big deal. – Bowlesas.character(substitute(list(...)))[-1L]
(copying fromsave
). Looking at the mailing list response,deparse
is used rather thanas.character
. What are the pros/cons of the two approaches? – Emoryemote