[R] wrapper for save function

Erik Iverson iverson at biostat.wisc.edu
Tue Feb 26 18:07:35 CET 2008


Hello -

I would like to create a wrapper to the 'save' function in the base 
package, but have a small problem with the name of the object that is 
getting saved in the file.  Below is a simple example illustrating my 
problem.

## BEGIN SAMPLE R CODE

##################################################
## Here is the wrapper for the save function
##################################################

wrapsave <- function(mydata, ...) {
   save(mydata, ...)
}

##################################################
## Create a test data.frame and save it
##################################################

testdf <- data.frame(a = rnorm(10), b = rnorm(10))
wrapsave(testdf, file = "~/test.Rdata")

##################################################
## remove test data.frame and try to load it
##################################################

rm(testdf)
load(file = "~/test.Rdata")

## END SAMPLE R CODE

After the load function is called, an object called 'testdf' does not 
exist in the global environment, but an object called 'mydata' does. I 
understand why, but I'd like 'wrapsave' to save the object passed to it 
with the name of the object it was called with, in this case 'testdf'. 
Is this possible?

Thank you,
Erik Iverson



More information about the R-help mailing list