variable-scope in R functions

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-14 07:58:30

问题


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

回答1:


This is due to lazy evaluation. The function is not actually constructed until it is called. So, in the second case, both times the new version of a is picked up. See also this other question.

You can solve this issue by using force:

make.fn2 <- function(a, b) {
    force(a)
    force(b)
    fn2 <- function(x) {
        return( x + a + b )
    }
    return( fn2 )
}

This forces the function to pick up the values of a and b when the function is created, not when the function is called. It produces the correct output:

> a <- 2; b <- 3
> fn2.1 <- make.fn2(a, b)
> 
> a <- 4
> fn2.2 <- make.fn2(a, b)
> 
> fn2.1(3)    # 10
[1] 8
> fn2.1(4)    # 11
[1] 9
> fn2.2(3)    # 10
[1] 10
> fn2.1(3)    # 10
[1] 8
> 


来源:https://stackoverflow.com/questions/21628656/variable-scope-in-r-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!