title: Removing all but several items from the global environment
body: <p>I am trying to create a function to remove all but one to three items from the global environment. I am able to do it for one but not for two, three or more. Would appreciate any help on this using <code>base R</code> or <code>rlang</code></p>
  <pre class="lang-r prettyprint-override"><code>a &lt;- &quot;a&quot;
  b &lt;- c(1,2,3,4)
  c &lt;- c(&quot;M&quot;, &quot;F&quot;)
  remove_all_but &lt;- function(x){
    Sx &lt;- deparse(substitute(x))
    rm(list=Sx,envir=sys.frame(-1))
  }
  
  remove_all_but(a)
  
  remove_all_but(c(a, b))
  #&gt; Warning in rm(list = Sx, envir = sys.frame(-1)): object 'c(a, b)' not found
  </code></pre>