15

Possible Duplicate:
how to save() with a particular variable name

I'm wondering what an easy way is to save an object in R, using a variable objectName with the name of the object to be saved. I want this to easy save objects, with their name in the file name.

I tried to use get, but I didn't manage to save the object with it's original object name.

Example:

If I have the object called "temp", which I want to save in the directory "dataDir". I put the name of the object in the variable "objectName".

Attempt 1:

objectName<-"temp"
save(get(objectName), file=paste(dataDir, objectName, ".RData", sep=""))
load(paste(dataDir, objectName, ".RData", sep=""))

This didn't work, because R tries to save an object called get(objectName), instead of the result of this call. So I tried the following:

Attempt 2:

objectName<-"temp"
object<-get(objectName)
save(object, file=paste(dataDir, objectName, ".RData", sep=""))
load(paste(dataDir, objectName, ".RData", sep=""))

This obviously didn't work, because R saves the object with name "object", and not with name "temp". After loading I have a copy of "object", instead of "temp". (Yes, with the same contents...but that is not what I want :) ). So I thought it should be something with pointers. So tried the following:

Attempt 3:

objectName<-"temp"
object<<-get(objectName)
save(object, file=paste(dataDir, objectName, ".RData", sep=""))
load(paste(dataDir, objectName, ".RData", sep=""))

Same result as attempt 2. But I'm not sure I'm doing what I think I'm doing.

What is the solution for this?

0

2 Answers 2

31

Try save(list=objectName, file=paste(objectName, '.Rdata', sep='') ).

The key is that the list argument to save takes a list of character strings that is the names of the objects to save (rather than the actual objects passed through ...).

Sign up to request clarification or add additional context in comments.

1 Comment

You may also use paste0(objectName, '.RData'), which will save you a couple of strokes.
1

I found your examples hard to understand, but I can think of two possibilities of what you want. You either want the filename to be saved as objectName.RData or temp.RData. Here is how you do both:

objectName<-"temp"

# This saves the object as "temp.RData"
save(objectName, file=paste(dataDir, objectName, ".RData", sep=""))
# Loading it will bring it back with the name objectName, and the value temp
load(paste(dataDir, 'temp', '.RData', sep=''))

# This saves the object as "objectName.RData"
save(objectName, file=paste(dataDir, deparse(substitute(objectName)), ".RData",    sep=""))
# Loading it will bring it back with the name objectName, and the value temp
load(paste(dataDir, 'objectName', '.RData', sep=''))

All of your attempts return an error because you called the get incorrectly. It should have been get('objectName'), but if you think about it, that would get you exactly the same thing as objectName.

1 Comment

Hmm...I think my examples where not clear enough. It is not that I want to save the object "ObjectName", but the object called "temp". For instance; "temp" is a data.frame I want to save. Then I want to just do objectName <- "temp" and run for example the 'save' code line. (The reason is that I only have to change the value for object name, and all the save & load code (and some other stuff) do not need to change)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.