Comments (11)
The problem is that I have no control over <<-
- it's always going to work in the global environment, instead of the environment in which devtools is loading all the other code. Could you provide a bit more info about what you're doing with the globals? There might be a better way to set them up.
from devtools.
The code keeps a list of database environment variables and a key for
which to DB use. It's in the global environment since the package is
also used in some legacy code which sets these explicitly prior to the
package being loaded.
Here is the actual .onLoad function....
.onLoad <- function(lib, pkg, ...) {
## CONNECTION MANAGEMENT
if (!exists(".DB.USE",globalenv()))
.DB.USE <<- Sys.getenv("DB_USE", unset="PROD")
if (!exists(".DB.ENV",globalenv())) {
.DB.ENV <<- list()
.DB.ENV[[.DB.USE]] <<- db.env.get(.DB.USE)
}
}
I changed it to use the package namespace (following the example in
lubridate):
.DB.ENV <- NULL
.DB.USE <- NULL
.onLoad <- function(lib, pkg, ...) {
## CONNECTION MANAGEMENT
if (is.null(.DB.USE))
.DB.USE <<- Sys.getenv("DB_USE",unset="PGPROD")
if (is.null(.DB.ENV)) {
.DB.ENV <<- list()
.DB.ENV[[.DB.USE]] <<- db.env.get(.DB.USE)
}
}
Which is cleaner, works, and is no doubt the right answer but that
means I needed to explicitly unlock the bindings for .DB.USE and
.DB.ENV, and the existing legacy code would need to be modified to
work properly (overdue cleanup so not terrible I guess).
It just surprised me that a package that worked fine via library would not load.
Looking at devtools code I am guessing the issue is that when you create
the environment you don't assign a parent frame and then do the attach
before sourcing the code rather than sourcing then attaching.
from devtools.
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.
from devtools.
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?
from devtools.
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
something else. Someone with much more R internals experience would have to answer that one.
from devtools.
Here is a simple example of what I am talking about
In /tmp/x.R:
ZZ <<- 1
cat("found ZZ in",find("ZZ"),"\n")
cat("ZZ=",ZZ,"\n")
then run:
e1 <- new.env(parent = emptyenv())
attach(e1, name="E1")
e2 <- as.environment("E1")
e3 <- new.env(parent = globalenv())
attach(e3, name="E3")
e4 <- as.environment("E3")
for (e in c("e1","e2","e3","e4")) {
cat("\n",e,"\n")
print(get(e))
try(sys.source("/tmp/x.R", env=get(e)))
try(rm("ZZ", envir=globalenv()))
}
Which for me produced:
e1
<environment: 0x2873178>
Error in eval(expr, envir, enclos) : could not find function "<<-"
e2
<environment: 0x28728a0>
attr(,"name")
[1] "E1"
found ZZ in .GlobalEnv
Error in cat("ZZ=", ZZ, "\n") : object 'ZZ' not found
In addition: Warning message:
In rm("ZZ", envir = globalenv()) : object 'ZZ' not found
e3
<environment: 0x1fc4160>
found ZZ in .GlobalEnv
ZZ= 1
e4
<environment: 0x1fc38f8>
attr(,"name")
[1] "E3"
found ZZ in .GlobalEnv
Error in cat("ZZ=", ZZ, "\n") : object 'ZZ' not found
from devtools.
Thanks Jeff - I'll incorporate that change when I get a chance
from devtools.
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,
from devtools.
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.
from devtools.
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!
from devtools.
Fixed in #126.
from devtools.
Related Issues (20)
- Simpler `test_coverage_active_file()` implementation
- Upkeep for devtools (2023)
- Add dev_example() to documentation for run_examples() HOT 3
- `release` causes me a problem if 'foghorn' package is installed HOT 1
- Using system.file in a tryCatch environment HOT 2
- Export is not working when using typed library HOT 3
- Solution to the "checking examples ... ERROR" HOT 1
- [feature request] a new `exclude` argument into `run_examples()` HOT 2
- Clearly list system dependencies in documentation HOT 1
- `load_all()` documentation refers to `is_loading()`
- devtools::check(error_on="warning") doesn't error on warnings from documenting HOT 4
- `run_examples()` fails if `tests/testthat/helper.R` contains an un-exported function
- Update README reg R Packages second edition
- .
- Can't install the package HOT 2
- can not install devtools in R4.4.1 HOT 4
- do not ask about `check_rhub()` in `release()` because it is deprecated and defunct
- Deprecate `install_*`
- Import pak
- Use `httr` conditionally
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from devtools.