In the book, the exercise is on page 10/23 of the Environments
chapter, after section Iteration vs. Recursion
. It is
Modify
where()
to find all environments that contain a binding forname
.
Here, where()
is from the pryr
package. First of all, to be sure I understand what is asked: Say I have the name mean
. This could refer to:
> mean
function (x, ...)
UseMethod("mean")
<bytecode: 0x2234b58>
<environment: namespace:base>
But also, say I assign a value to a variable of the same name:
> mean <- 3
> mean
[1] 3
So, now (please correct me if I'm wrong), the former mean
is bound by the baseenv()
whereas the latter is bound by globalenv()
. Correct?
> ls(as.environment(globalenv()))
[1] "mean"
> which(ls(as.environment(baseenv()))=="mean")
[1] 671
So I wrote:
where2 <- function(k, name, env) {
stopifnot(is.character(name), length(name) == 1)
# Why does this only work when calling 'where' from
# the 'pryr' package?
# env <- to_env(env)
# Hopefully the same as 'to_env'.
# env <- as.environment(env)
# Successful case.
if(exists(name, env, inherits=FALSE)) {
k <- list(k, env)
where2(k, name, parent.env(env))
}
# Base case or search one level up.
if(identical(env, emptyenv())) {
stop("Can't find ", name, call.=FALSE)
} else {
where2(k, name, parent.env(env))
}
}
inspired by the where
function from the pryr
package.
I was hoping I could now do (at the R prompt):
> source("./where2.r")
> mean <- 3
> k <- list()
> where2(k, "mean", parent.frame())
Error: Can't find mean
and get a list containint the base- and global environments.
What should I do differently and how?