title: Cleaning up the global environment after sourcing: How to remove objects of a certain type in R
body: <p>I read in a public-use dataset that created dozens of temporary vectors in the process of building a final dataframe. Since this dataframe will be analyzed as part of a larger process, I plan on <code>source</code>ing the R script that creates the dataframe, but do not want to leave myself or future users with a cluttered global environment. </p>
  
  <p>I know that I can use <code>ls</code> to list the current objects in my global environment and use <code>rm</code> to remove certain objects, but I'm unsure of how to use those two functions in concert <em><strong>to remove all objects except the dataframe created by a certain script</strong></em>.</p>
  
  <p>To clarify, here is a reproducible example:</p>
  
  <p><strong>Script 1, named "script1.R"</strong></p>
  
  <pre><code>setwd("C:/R/project")
  set.seed(12345)
  var &lt;- letters
  for (i in var) {
    assign(i, runif(1))
  }
  df &lt;- data.frame(x1 = a, x2 = b, x3 = c)
  </code></pre>
  
  <p><strong>Script 2</strong></p>
  
  <pre><code>source("script1.r")
  </code></pre>
  
  <p>It would be easy enough to remove all vectors from the <code>source</code>d script by some combination of <code>rm</code>, <code>ls</code> with <code>pattern = letters</code> or something like that, but what I want to do is create a general function that removes ALL vectors created by a certain script and only retain the dataframe (in this example, <code>df</code>).</p>
  
  <p>(<strong>NOTE</strong>: There are similar questions as this <strong><a href="https://stackoverflow.com/questions/2914792/what-ways-are-there-for-cleaning-an-r-environment-from-objects">here</a></strong> and <strong><a href="https://stackoverflow.com/questions/28142088/how-to-exclude-only-the-data-frames-from-the-global-environment-in-r">here</a></strong>, but I feel mine is different in that it is more specific to sourcing and cleaning in the context of a multi-script project). </p>
  
  <p><strong>Update</strong>
  While looking around, the following link gave me a nice work around:</p>
  
  <p><a href="https://stackoverflow.com/questions/2822532/how-can-i-neatly-clean-my-r-workspace-while-preserving-certain-objects">How can I neatly clean my R workspace while preserving certain objects?</a></p>
  
  <p>Specifically, user @Fojtasek suggested:</p>
  
  <blockquote>
    <p>I would approach this by making a separate environment in which to store all the junk variables, making your data frame using with(), then copying the ones you want to keep into the main environment. This has the advantage of being tidy, but also keeping all your objects around in case you want to look at them again.</p>
  </blockquote>
  
  <p>So I could just append the source code that creates the dataframe as follows...</p>
  
  <pre><code>temp &lt;- new.env()
  with(temp, {
      var &lt;- letters
  for (i in var) {
    assign(i, runif(1))
  }
  df &lt;- data.frame(x1 = a, x2 = b, x3 = c)
  }
  </code></pre>
  
  <p>... and then just extract the desired dataframe (<code>df</code>) to my global environment, but I'm curious if there are other elegant solutions, or if I'm thinking about this incorrectly. </p>
  
  <p>Thanks.</p>