I'd like to specify functions in a flexible way. How can I make sure that the environment of a given function does not change when I create another function just after it.
To illustrate, this works properly:
make.fn2 <- function(a, b) {
fn2 <- function(x) {
return( x + a + b )
}
return( fn2 )
}
a <- 2; b <- 3
fn2.1 <- make.fn2(a, b)
fn2.1(3) # 8
fn2.1(4) # 9
a <- 4
fn2.2 <- make.fn2(a, b)
fn2.2(3) # 10
fn2.1(3) # 8
This does not
make.fn2 <- function(a, b) {
fn2 <- function(x) {
return( x + a + b )
}
return( fn2 )
}
a <- 2; b <- 3
fn2.1 <- make.fn2(a, b)
a <- 4
fn2.2 <- make.fn2(a, b)
fn2.1(3) # 10
fn2.1(4) # 11
fn2.2(3) # 10
fn2.1(3) # 10