1

For instance, I have an environment myEnv which lives in my package Test. So why on earth does getPackageName(myEnv) return the current time "2014-02-03 17:17:23" instead of "Test"??

# In /R/Test.R
myEnv <- new.env()
print(getPackageName(myEnv))

# Now build in RStudio:
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source Test

<other messages here>

** preparing package for lazy loading
[1] "2014-02-03 17:17:23"
Warning in getPackageName(myEnv) :
  Created a package name, '2014-02-03 17:17:23', when none found

<etc etc etc>

I don't see this behaviour or its reasoning documented anywhere. Indeed, this can wreak havoc, as clearly demonstrated by this question, hence every time I create an environment I have to remember to do something like setPackageName("Test", myEnv) to associate it with my package.

This just seems superfluous and unnecessary, so why have this behaviour?

Community
  • 1
  • 1
mchen
  • 9,808
  • 17
  • 72
  • 125

1 Answers1

0

Try getting the parent environment of your env instead of the environment itself.

This seems to work at least for a simple example I'm installing using devtools, so who knows what tricks Hadley does with environments in there:

> parent.env(myEnv)
<environment: namespace:Test>
> getPackageName(parent.env(myEnv))
[1] "Test"

This doesn't work for other objects defined in the package:

> foo
function(){
}
<environment: namespace:Test>
> getPackageName(parent.env(foo))
Error in parent.env(foo) : argument is not an environment

you just have to get the environment thus:

> getPackageName(environment(foo))
[1] "Test"
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Thanks - an interesting comment, but this somewhat misses my original point. To clarify, I'm not actually interested in *how* to get the package name - I'm interested in *why* `myEnv` isn't associated with the package its defined in to begin with. I simply used `getPackageName(myEnv)` as an example to highlight this behaviour. The need, as you point out, to make an extra call `parent.env` is a consequence of this behaviour. – mchen Feb 03 '14 at 19:06
  • Its because the argument to `getPackageName` is the environment *of* the package, and you were just feeding it an environment which was *in* the package. – Spacedman Feb 03 '14 at 23:48