可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
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.
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()
).
Thanks for the help!
回答1:
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:
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.
Another methods might be to use the "assign" function with the "get" function
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:
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:
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:
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...