为什么函数 A 主体中的变量查找从全局环境中获取值,而不是调用 A 的函数 B 中的值?

2023-12-25

我定义了一个函数:

.get <- function( o, ...) {
    p <- match.call( expand.dots = 0)$...
    cat( sprintf( 'In .get, it is %s.\n', eval( tail( p, 1)[[ 1]])))
    fn <- switch( typeof( o), list =, environment = `[[`, 'S4' = '@', `[`)
    if( length( p)) eval( as.call( c( fn, quote( o), p))) else o # Here when true, I compose a call based on p.
}

然后我尝试了如下:

it <- 1
m <- matrix( seq( 9), 3)
sapply( seq( 3), function( it) {
    cat( sprintf( 'In sapply, it is: %s.\n', it))
    .get( m, , it)
})
sapply( seq( 3), function( it) .get( m, , it))

输出:

In sapply, it is: 1.
In .get, it is 1.
In sapply, it is: 2.
In .get, it is 1.
In sapply, it is: 3.
In .get, it is 1.
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3

但预期输出是:

In sapply, it is: 1.
In .get, it is 1.
In sapply, it is: 2.
In .get, it is 2.
In sapply, it is: 3.
In .get, it is 3.
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

那么为什么是it不是 1 到 3(调用函数时的值),而是始终在全局环境中分配的值(即 1)?


你定义了吗get在全球环境中与it?如果是这样,那么这可能是一个范围界定问题。看here http://darrenjw.wordpress.com/2011/11/23/lexical-scope-and-function-closures-in-r/ and here http://www.r-loggers.com/closures-in-r-a-useful-abstraction/进行精彩的讨论。

从第一个链接看这个例子:

 a = 1
 b = 2

 fun <- function(x){ a + b*x }

 new.fun <- function(x){
 a = 2
 b = 1
 fun(x)
 }

 new.fun(2)

If fun内部调用new.fun uses a and b从全球环境来看,我们预期的结果是new.fun(2) to be 1+2*2=5而如果它使用函数中定义的参数new.fun,那么应该是2+1*2=4。现在,大多数人预计结果是 4,但结果却是 5。为什么呢?因为fun是在全局环境中定义的,因此全局变量a and b重要的是fun。要看到这一点,您可以使用以下命令查看函数的结构str(fun)这将揭示该函数附加了一个环境。观察那个环境list(environment(fun)),您将看到该函数“记住”它是在全局环境中定义的。由于这个原因,函数fun首先会在那里查找参数a and b.

为了解决这个问题,已经提出了许多解决方法,如果您通过谷歌搜索词法作用域可以找到其中的一些解决方法。有关背景信息,Hadley Wickam 即将出版的书中有关于环境的精彩部分,请参阅here http://adv-r.had.co.nz/Environments.html。对于潜在的解决方案,请参见,例如here https://stackoverflow.com/questions/2646402/using-functions-and-environments。解决问题的一种方法是覆盖环境。例如,

 new.fun2 <- function(x){
 a = 2
 b = 1
 environment(fun) = environment()
 fun(x)
 }

 new.fun2(2)

现在给出 4 作为答案,使用a=2, b=1在父环境中定义,而不是在全局环境中。我相信还有很多更优雅的解决方案。

也就是说,在你的情况下,使用

 sapply( seq( 3), function(it) {
   cat( sprintf( 'In sapply, it is: %s.\n', it))
   environment(.get) <- environment()
   .get( m, , it)
 })

works.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么函数 A 主体中的变量查找从全局环境中获取值,而不是调用 A 的函数 B 中的值? 的相关文章

随机推荐