title: r - Rename R object while save()-ing it
body: <p>I'm looking for a way to <code>save()</code> 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:</p>
  
  <pre><code>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...
  </code></pre>
  
  <p>Basically I would like the <code>save()</code> function to take the value of <code>XXX</code> and save it to file under a variable named <code>AAA</code>. Note that this is not a question about renaming a variable: I could of course rename the variable <code>XXX</code> prior to saving, e.g. <code>AAA = XXX</code> and then <code>save(AAA, ..., file=...)</code> but this would of course mess up with the value of <code>AAA</code> in the rest of the code. </p>
  
  <p>The obvious way is to create temporary variables and then restore the values:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>... but everyone will agree that this is quite messy (especially with many more variables).</p>
  
  <p>This has been bugging me for a while, and my feeling is that the function <code>save()</code> 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. <code>saveRDS()</code>).</p>
  
  <p>Thanks for the help!</p>