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.
我想知道一个简单的方法是使用变量objectName和要保存的对象的名称来保存R中的对象。我希望这可以轻松保存对象,其名称在文件名中。
I tried to use get
, but I didn't manage to save the object with it's original object name.
我试图使用get,但我没有设法用它的原始对象名称保存对象。
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".
如果我有一个名为“temp”的对象,我想将其保存在“dataDir”目录中。我将对象的名称放在变量“objectName”中。
Attempt 1:
尝试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:
这不起作用,因为R试图保存一个名为get(objectName)的对象,而不是这个调用的结果。所以我尝试了以下内容:
Attempt 2:
尝试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:
这显然不起作用,因为R保存名为“object”的对象,而不是名称“temp”。加载后我有一个“对象”的副本,而不是“临时”。 (是的,内容相同......但这不是我想要的:))。所以我认为它应该是指针的东西。所以尝试了以下内容:
Attempt 3:
尝试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.
与尝试2相同的结果。但我不确定我在做我认为我正在做的事情。
What is the solution for this?
这是什么解决方案?
2 个解决方案
#1
17
Try save(list=objectName, file=paste(objectName, '.Rdata', sep='') )
.
尝试保存(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 ...
).
关键是要保存的list参数采用一个字符串列表,这些字符串是要保存的对象的名称(而不是通过的实际对象...)。
#2
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.RData或temp.RData。以下是您如何做到这两点:
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
.
所有尝试都会返回错误,因为您错误地调用了get。它应该是get('objectName'),但是如果你考虑它,那将会得到与objectName完全相同的东西。
#1
17
Try save(list=objectName, file=paste(objectName, '.Rdata', sep='') )
.
尝试保存(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 ...
).
关键是要保存的list参数采用一个字符串列表,这些字符串是要保存的对象的名称(而不是通过的实际对象...)。
#2
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.RData或temp.RData。以下是您如何做到这两点:
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
.
所有尝试都会返回错误,因为您错误地调用了get。它应该是get('objectName'),但是如果你考虑它,那将会得到与objectName完全相同的东西。