New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
problem setting globals in .onLoad() #3
Comments
The problem is that I have no control over |
The code keeps a list of database environment variables and a key for Here is the actual .onLoad function....
I changed it to use the package namespace (following the example in
Which is cleaner, works, and is no doubt the right answer but that It just surprised me that a package that worked fine via library would not load. |
I accidentally closed this. I made the change to create the environment, source, then attach and then the sample code works. Take a look at jcdny/devtools@025c6504c1907e37accd for what I am talking about. |
You've made two changes in that code - one to source then attach, and the other to use global environment as the parent. Would you mind testing to figure out which is the important change? |
You need to do both things. You need to provide the enclosing environment when doing the source and when you attach it actually makes a copy of the environment and I think discards the enclosing environment. I've messed around with it a bit and nothing seems to break with that change though I admit it was all a bit cargo culted and it's not clear if the env should be globalenv() or |
Here is a simple example of what I am talking about In /tmp/x.R: then run:
Which for me produced:
|
Thanks Jeff - I'll incorporate that change when I get a chance |
Hmmmm, this change breaks with S4 because of the way method tables work - the environment needs to be attached before the class definition functions are run, |
Ah, I tested with a number of packages but nothing with S4 methods. That's unfortunate. I will think about it more and if I get any bright ideas I will let you know. I suspect to support namespaces you will need to load packages more like the existing code does and that might be the only way to get both of these. |
Yes, it's hard to replicate regular package loading exactly because so many of the key functions are internal, and nothing is really documented. One day I'll be able to do better! |
Fixed in #126. |
I have a package which sets some globals in the .onLoad function but when I try to load the package with devtools I get an error trying to reference the global variables within the .onLoad function itself.
simple example:
Produces the following error in R
if I comment out the second line the code loads fine and XX is set in the global environment.
The package itself passes R CMD check with no errors an loads fine when installed...
The text was updated successfully, but these errors were encountered: