title: Saving a single object within a function in R: RData file size is very large
body: <p>I am trying to save trimmed-down GLM objects in R (i.e. with all the "non-essential" characteristics set to NULL e.g. residuals, prior.weights, qr$qr).</p>
  
  <p>As an example, looking at the smallest object that I need to do this with:</p>
  
  <pre><code>print(object.size(glmObject))
  168992 bytes
  save(glmObject, "FileName.RData")
  </code></pre>
  
  <p>Assigning this object in the global environment and saving leads to an RData file of about 6KB. </p>
  
  <p>However, I effectively need to create and save the glm object within a function, which is in itself within a function. So the code looks something like:</p>
  
  <pre><code>subFn &lt;- function(DT, otherArg, ...){
                   glmObject &lt;- glm(...)
                   save(glmObject,"FileName.RData")
  }
  
  mainFn &lt;- function(DT, ...){ 
               subFn(DT, otherArg, ...)
  }
  
  mainFn(DT, ...)
  </code></pre>
  
  <p>Which leads to much, much larger RData files of about 20 MB, despite the object itself being the same size.</p>
  
  <p>So I understand this to be an environment issue, but I'm struggling to pinpoint exactly how and why it's happening. The resulting file size seems to vary quite a lot. I have tried using saveRDS, and equally I have tried assigning the glmObject via &lt;&lt;- to make it global, but nothing seems to help. </p>
  
  <p>My understanding of environments in R clearly isn't very good, and would really appreciate if anyone could suggest a way around this. Thanks.</p>