fa <- function(x){x+1}
fb <- function(x){x-1}
f1 <- function(x, y){f(x)^y}
f2 <- function(x, ab, y){
if(ab == 'a'){
f <- fa
} else {
f <- fb
}
f1(x, y)
}
f2(0, 'a', .5)
Error in f1(x, y) : could not find function "f"
The above doesn't work because f
isn't defined in f1
's environment.
What is a good way to make this work? That
- Avoids passing everything in the global environment to
f2
's environment - Avoids having to redefine the function inside
f2
(this would be a hassle and create opportunities for copy/pasting error)
Would it make sense to define some sort of "subglobal" environment, and put things that I want everybody to use in this environment, and then make every function be able to access things from "subglobal"? And then somehow make sure that subglobal is always a strict subset of global? If sensible, how would I do this?