title: Why/how some packages define their functions in nameless environment?
body: <p>In my code, I needed to check which package the function is defined from (in my case it was <code>exprs()</code>: I needed it from <code>Biobase</code> but it turned out to be overriden by <code>rlang</code>).<br>
  From <a href="https://stackoverflow.com/questions/44696431/how-to-get-the-package-name-of-a-function-in-r">this SO question</a>, I thought I could use simply <code>environmentName(environment(functionname))</code>. But for <code>exprs</code> from <code>Biobase</code> that expression returned empty string:</p>
  
  <pre><code>environmentName(environment(exprs))
  # [1] ""
  </code></pre>
  
  <p>After checking the structure of <code>environment(exprs)</code> I noticed that it has <code>.Generic</code> member which contains package name as an attribute:</p>
  
  <pre><code>environment(exprs)$.Generic
  # [1] "exprs"
  # attr(,"package")
  # [1] "Biobase"
  </code></pre>
  
  <p>So, for now I made this helper function:</p>
  
  <pre><code>pkgparent &lt;- function(functionObj) {
    functionEnv &lt;- environment(functionObj)
    envName &lt;- environmentName(functionEnv)
    if (envName!="") 
      return(envName) else
        return(attr(functionEnv$.Generic,'package'))
  }
  </code></pre>
  
  <p>It does the job and correctly returns package name for the function if it is loaded, for example:</p>
  
  <pre><code>pkgparent(exprs)
  # Error in environment(functionObj) : object 'exprs' not found
  
  library(Biobase)
  
  pkgparent(exprs)
  # [1] "Biobase"
  
  library(rlang)
  # The following object is masked from ‘package:Biobase’:
  #   exprs
  
  pkgparent(exprs)
  # [1] "rlang"
  </code></pre>
  
  <p><strong>But I still would like to learn how does it happen that for some packages their functions are defined in "unnamed" environment while others will look like <code>&lt;environment: namespace:packagename&gt;</code>.</strong></p>