-
Notifications
You must be signed in to change notification settings - Fork 758
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
Support for namespace and package environments #126
Conversation
} | ||
|
||
copy_env <- function(src, dest) { | ||
src_objs <- ls(envir = src) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if list2env(as.list(src), dest)
is a better idiom for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also be careful about objects starting with .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, got the '.' objects. The list2env
isn't appropriate here, since the environment is already created by the makeNamespace
function.
I guess we'll need some tests for this too :( |
Writing tests for this will be a challenge. What do you think about making a another package inside of devtools, specifically for running tests on? Maybe it could go in |
The imports also need to be properly loaded. They should go in the parent environment of For example, if you run this code when loading via # Print parent frames, and return last one printed
# e: the environment to start in
# n: max number of levels envs to print
# p: print all the environments? If FALSE, just return last frame
printenvs <- function(e = parent.frame(), n = 100, p = TRUE) {
if (p) cat(str(e, give.attr=F))
i <- 1
while(i < n) {
if (identical(e, emptyenv())) break
e <- parent.env(e)
if (p) cat(str(e, give.attr=F))
i <- i+1
}
cat("\n")
return(e)
}
library(ggplot2)
debug(qplot)
qplot()
# Now at browser prompt
e <- printenvs(n=3)
# <environment: 0x104153e28>
# <environment: namespace:ggplot2>
# <environment: 0x103162bf8>
e
# <environment: 0x103162bf8>
# attr(,"name")
# [1] "imports:ggplot2"
ls(e)
# Shows all the objects from ggplot2 imports, like grid, scales, plyr The imported packages are loaded into their appropriate namespaces, but the do not have the associated true
# function (...)
# TRUE
# <environment: namespace:plyr>
debug(true)
true()
# At debug prompt for true()
# Show all parent envs
printenvs(n=5)
# <environment: 0x10360a8a8>
# <environment: namespace:plyr>
# <environment: 0x102f37438>
# <environment: namespace:base>
# <environment: R_GlobalEnv>
# <environment: package:ggplot2>
# <environment: package:stats>
# <environment: package:graphics>
# <environment: package:grDevices>
# <environment: package:utils>
# <environment: package:datasets>
# <environment: package:methods>
# <environment: 0x10093a2e8>
# <environment: base>
# <environment: R_EmptyEnv> So the imported packages are loaded into their namespace, and then their exported objects are copied over to the Also see: |
|
||
|
||
# Took this from base::loadNamespace() | ||
makeNamespace <- function(name, version = NULL, lib = NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this function directly from the inside of base::loadNamespace()
in R 2.15.1. I don't know if it's correct for other versions of R.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably won't be - but I think that's ok.
It now imports objects in to the imports environment (instead of loading the dependency packages with Now I'm getting a strange warning in
This warning happens in the tryCatch(
lapply(paths, sys.source, envir = env, chdir = TRUE,
keep.source = TRUE),
error = function(e) {
clear_cache()
stop(e)
} When I replace the for (path in paths) {
sys.source(path, envir = env, chdir = TRUE, keep.source = TRUE)
} But if I use a loop that uses numerical indexing, the warning doesn't happen! for (i in length(paths)) {
sys.source(paths[i], envir = env, chdir = TRUE, keep.source = TRUE)
} |
Conflicts: R/load-code.r R/load.r
invisible(deps) | ||
} | ||
|
||
#' Parse dependencies. | ||
#' @return character vector of package names | ||
#' Load a dependency package. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A "dependent" package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't "x is dependent" mean that x depends on something else, as in "John is dependent on nicotine".
"x is a dependency of y" means that y depends on x. It's slightly awkward, but you could say, "nicotine is a dependency of John's".
The new list of objects to ignore is taken from base::loadNamespace.
just the exported ones) are still copied to the package environment. | ||
(Winston Chang. Fixes #3, #60, and #125) | ||
|
||
* Packages listed in Imports, Suggests, and Depends are now loaded into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are suggests still loaded in here? If so, they probably shouldn't be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
Previously if there was an error loading the code, the namespace would still exist. This could cause problems when attempting to load the package again.
This is a first attempt at loading the package into a namespace. It works, but the code seriously needs to be cleaned up.
<namespace:ggplot2>
There is also a package environment.
<package:ggplot2>
This is really rough and there will probably be problems unloading namespaces.
Here's the first 8 entries in the environment stack when loading ggplot2 with
library()
and debuggingqplot()
:And when loading via the modified
load_all()
:It looks pretty much the same, although the order seems to be different for the dependency packages (those below ggplot2). I think when loading with
library()
, those are only visible from the namespace; they're not attached and visible from the global environment.