I'm looking for a way to save()
a variable under a different name "on the fly" in R (bear with me! I'm pretty sure that's not a duplicate...). Here is an example of what I'd like to achieve:
我正在寻找一种方法来保存()一个变量在一个不同的名称下“在飞行中”在R中(跟我一起!我很确定这不是重复......)。这是我想要实现的一个例子:
AAA = 1
BBB = 2
XXX = 3
YYY = 4
save(AAA=XXX, BBB=YYY, file="tmp.Rdat")
# does NOT save a variable AAA to file with value 3 in it, which is the aim...
Basically I would like the save()
function to take the value of XXX
and save it to file under a variable named AAA
. Note that this is not a question about renaming a variable: I could of course rename the variable XXX
prior to saving, e.g. AAA = XXX
and then save(AAA, ..., file=...)
but this would of course mess up with the value of AAA
in the rest of the code.
基本上我希望save()函数取值XXX并将其保存到名为AAA的变量下的文件中。请注意,这不是关于重命名变量的问题:我当然可以在保存之前重命名变量XXX,例如AAA = XXX然后保存(AAA,...,file = ...)但这当然会在其余代码中弄乱AAA的值。
The obvious way is to create temporary variables and then restore the values:
显而易见的方法是创建临时变量,然后恢复值:
AAA = 1
BBB = 2
XXX = 3
YYY = 4
AAAtmp = AAA; BBBtmp = BBB # record values of AAA, BBB
AAA = XXX; BBB = YYY
save(AAA, BBB, file="tmp.Rdat")
AAA = AAAtmp; BBB = BBBtmp # restore values of AAA, BBB
... but everyone will agree that this is quite messy (especially with many more variables).
......但是每个人都会同意这是非常混乱的(特别是有更多的变量)。
This has been bugging me for a while, and my feeling is that the function save()
can't do what I want. So I guess I will have to update my code and go down the path of using a different saving function (e.g. saveRDS()
).
这已经困扰了我一段时间,我的感觉是函数save()不能做我想要的。所以我想我将不得不更新我的代码并沿着使用不同保存功能的路径(例如saveRDS())。
Thanks for the help!
谢谢您的帮助!
4 个解决方案
#1
9
This proved to be a little trickier that I expected. I'll be interested to see what others come up with, and also what any objections to my solution may be.
事实证明这比我预期的要复杂一些。我有兴趣看看其他人提出了什么,以及对我的解决方案有什么异议。
saveit <- function(..., file) {
x <- list(...)
save(list=names(x), file=file, envir=list2env(x))
}
foo <- 1
saveit(bar=foo, file="hi.Rdata")
#2
0
I found that I can create a list and assign all objects in the environment to each element of the list using the "get" function and then i can rename each element of the list.
我发现我可以使用“get”函数创建一个列表并将环境中的所有对象分配给列表的每个元素,然后我可以重命名列表的每个元素。
Another methods might be to use the "assign" function with the "get" function
另一种方法可能是将“assign”函数与“get”函数一起使用
objectNames <- ls(all.names=T)
res <- list()
for (o in objectNames) {
res[[paste(prefix,o,sep="_")]] <- get(o)
}
format(object.size(res), units="Gb")
#3
0
A bit quicker than defining a function for the job, you can create a local
environment:
比定义作业的功能快一点,您可以创建一个本地环境:
local({
AAA <- XXX
BBB <- YYY
save(AAA, BBB, file="tmp.Rdat")
})
Because you are working and assigning to a different environment, you don't need to store temporary variables that will still be intact in the global environment:
因为您正在工作并分配到不同的环境,所以您不需要存储在全局环境中仍然完好无损的临时变量:
> AAA
[1] 1
> BBB
[1] 2
#4
0
A maybe helpful addition to Aarons answer:
If you want to save and load files with a filename you define as a string previously , consider this small modification of Aarons (very cool!) answer:
如果你想保存和加载你之前定义为字符串的文件名的文件,请考虑Aarons的这个小修改(非常酷!)答案:
model_name <- "model1"
saveit <- function(..., string, file) {
x <- list(...)
names(x) <- string
save(list=names(x), file=file, envir=list2env(x))
}
fit <- somemodelingfunc(...)
saveit(fit = fit, string = model_name, file=paste(model_name, ".RData", sep=""))
This will adds functionality because you can pass filenames as strings without using the variables directly, which is useful if your saves-and-loads need to have a specific name that depends on context. My use case are model fits, for example where I define just the model name in the beginning of the file, but don't need to modify anything else in the script. I haven't seen a easier way to do this in R...
这将添加功能,因为您可以将文件名作为字符串传递而不直接使用变量,如果您的存储和加载需要具有依赖于上下文的特定名称,这将非常有用。我的用例是模型拟合,例如我在文件开头只定义模型名称,但不需要修改脚本中的任何其他内容。我还没有看到更简单的方法在R ...
#1
9
This proved to be a little trickier that I expected. I'll be interested to see what others come up with, and also what any objections to my solution may be.
事实证明这比我预期的要复杂一些。我有兴趣看看其他人提出了什么,以及对我的解决方案有什么异议。
saveit <- function(..., file) {
x <- list(...)
save(list=names(x), file=file, envir=list2env(x))
}
foo <- 1
saveit(bar=foo, file="hi.Rdata")
#2
0
I found that I can create a list and assign all objects in the environment to each element of the list using the "get" function and then i can rename each element of the list.
我发现我可以使用“get”函数创建一个列表并将环境中的所有对象分配给列表的每个元素,然后我可以重命名列表的每个元素。
Another methods might be to use the "assign" function with the "get" function
另一种方法可能是将“assign”函数与“get”函数一起使用
objectNames <- ls(all.names=T)
res <- list()
for (o in objectNames) {
res[[paste(prefix,o,sep="_")]] <- get(o)
}
format(object.size(res), units="Gb")
#3
0
A bit quicker than defining a function for the job, you can create a local
environment:
比定义作业的功能快一点,您可以创建一个本地环境:
local({
AAA <- XXX
BBB <- YYY
save(AAA, BBB, file="tmp.Rdat")
})
Because you are working and assigning to a different environment, you don't need to store temporary variables that will still be intact in the global environment:
因为您正在工作并分配到不同的环境,所以您不需要存储在全局环境中仍然完好无损的临时变量:
> AAA
[1] 1
> BBB
[1] 2
#4
0
A maybe helpful addition to Aarons answer:
If you want to save and load files with a filename you define as a string previously , consider this small modification of Aarons (very cool!) answer:
如果你想保存和加载你之前定义为字符串的文件名的文件,请考虑Aarons的这个小修改(非常酷!)答案:
model_name <- "model1"
saveit <- function(..., string, file) {
x <- list(...)
names(x) <- string
save(list=names(x), file=file, envir=list2env(x))
}
fit <- somemodelingfunc(...)
saveit(fit = fit, string = model_name, file=paste(model_name, ".RData", sep=""))
This will adds functionality because you can pass filenames as strings without using the variables directly, which is useful if your saves-and-loads need to have a specific name that depends on context. My use case are model fits, for example where I define just the model name in the beginning of the file, but don't need to modify anything else in the script. I haven't seen a easier way to do this in R...
这将添加功能,因为您可以将文件名作为字符串传递而不直接使用变量,如果您的存储和加载需要具有依赖于上下文的特定名称,这将非常有用。我的用例是模型拟合,例如我在文件开头只定义模型名称,但不需要修改脚本中的任何其他内容。我还没有看到更简单的方法在R ...