title: Which function will identify the name of an R variable's enclosing environment?
body: <p>I've been reading about R environments, and I'm trying to test my understanding with a simple example:</p>
  
  <pre><code>&gt; f &lt;- function() {
  +   x &lt;- 1
  +   environment(x)
  + }
  &gt; 
  &gt; f()
  NULL
  </code></pre>
  
  <p>I'm assuming this means that the object x is enclosed by the environment named NULL, but when I try to list all the objects in that environment, R displays an error message:</p>
  
  <pre><code>&gt; ls(NULL)
  Error in as.environment(pos) : using 'as.environment(NULL)' is defunct
  </code></pre>
  
  <p>So I'm wondering if there's a built-in function I can use on the command line that will return the environment name given the object name.  I tried this:</p>
  
  <pre><code>&gt; environment(x)
  Error in environment(x) : object 'x' not found
  </code></pre>
  
  <p>but that returned an error as well.  Any help will be greatly appreciated.</p>