Sourcing this code:
a <- F
f1 <- function() a
f2 <- function() {
a <- T
eval(f1())
}
and calling f2() will return FALSE.
How to modify the arguments for the eval so that f2() will return TRUE ?
You can do this:
a <- F
f1 <- function() a
f2 <- function() {
a <- T
environment(f1) <- new.env()
eval(f1())
}
f2()
# [1] TRUE
Though I wouldn't encourage it. What we've done here is changed the environment of f1 to be one which has for enclosure f2's environment, which means f1 will have access to f2s variables through "lexical" scoping (well, faux-lexical here b/c we short circuited it).
Generally, as Roman suggests, you should explicitly pass arguments to functions as otherwise you can quickly run into trouble.
f1withaas argument is because my f1 is in factOps.myclass, which hasOps(e1, e2)signature, notOps(e1, e2, a)signature. What I want to achieve is for Ops to operate on my class with different behaviors depending on value ofaais global, if it is in your real code as well you could assign a using<<-inf2. Or use theassignfunction to a particular environment...myclass, why don't you just add a slot to containaand pass it that way, or even better, define different sub-classes that invoke different methods depending on what type ofathey belong to?aclash with the behavior off2, which depends on the locala