Save object using variable with object name [duplicate]

折月煮酒 提交于 2019-11-30 07:10:39

问题


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?


回答1:


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 ...).




回答2:


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.



来源:https://stackoverflow.com/questions/11084395/save-object-using-variable-with-object-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!