title: Save object using variable with object name
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4675755/how-to-save-with-a-particular-variable-name">how to save() with a particular variable name</a>  </p>
  </blockquote>
  
  
  
  <p>I'm wondering what an easy way is to save an object in R, using a variable <code>objectName</code> with the name of the object to be saved. I want this to easy save objects, with their name in the file name.</p>
  
  <p>I tried to use <code>get</code>, but I didn't manage to save the object with it's original object name.</p>
  
  <p><strong>Example:</strong></p>
  
  <p>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".</p>
  
  <p><em>Attempt 1:</em></p>
  
  <pre><code>objectName&lt;-"temp"
  save(get(objectName), file=paste(dataDir, objectName, ".RData", sep=""))
  load(paste(dataDir, objectName, ".RData", sep=""))
  </code></pre>
  
  <p>This didn't work, because R tries to save an object called <code>get(objectName)</code>, instead of the result of this call. So I tried the following:</p>
  
  <p><em>Attempt 2:</em></p>
  
  <pre><code>objectName&lt;-"temp"
  object&lt;-get(objectName)
  save(object, file=paste(dataDir, objectName, ".RData", sep=""))
  load(paste(dataDir, objectName, ".RData", sep=""))
  </code></pre>
  
  <p>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:</p>
  
  <p><em>Attempt 3:</em></p>
  
  <pre><code>objectName&lt;-"temp"
  object&lt;&lt;-get(objectName)
  save(object, file=paste(dataDir, objectName, ".RData", sep=""))
  load(paste(dataDir, objectName, ".RData", sep=""))
  </code></pre>
  
  <p>Same result as attempt 2. But I'm not sure I'm doing what I think I'm doing. </p>
  
  <p>What is the solution for this?</p>