I am looking for a way to save() variable under a different name on the fly in R (bear with me! I'm sure this is not a duplicate ...). Here is an example of what I would like to achieve:
AAA = 1 BBB = 2 XXX = 3 YYY = 4 save(AAA=XXX, BBB=YYY, file="tmp.Rdat")
Basically, I would like the save() function to take the value XXX and save it in a file under a variable called AAA . Note that this is not a matter of renaming a variable: I could, of course, rename the variable XXX before saving, for example. AAA = XXX and then save(AAA, ..., file=...) , but that of course will ruin the AAA value 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 pretty dirty (especially with many other variables).
This has been undermining me for a while, and I feel that the save() function cannot do what I want. Therefore, I assume that I will have to update my code and follow the path of using another save function (for example, saveRDS() ).
Thanks for the help!
source share