gpt4 book ai didi

r - R 中的嵌套函数、sys.parent 和环境

转载 作者:行者123 更新时间:2023-12-04 07:39:32 25 4
gpt4 key购买 nike

这是我编写的一个奇怪的递归函数,用于帮助我理解 sys.parent() 和环境之间的关系:

dive = function(level = 1, max.depth = 5)
{
m = match.call()
print(paste("sys.frame() says", format(sys.frame())))
print(paste("sys.parent() says", sys.parent()))
print(paste("sys.frame(sys.parent()) says", format(sys.frame(sys.parent()))))
m[[2]] = m[[2]]+1
if(m[[2]] > max.depth)
stop("Not actually an error -- we're just tripping out!")
eval(m)
}

运行函数给出
dive(level = 1, max.depth = 5)

[1] "sys.frame() says <environment: R_GlobalEnv>"
[1] "sys.parent() says 0"
[1] "sys.frame(sys.parent()) says <environment: R_GlobalEnv>"
[1] "sys.frame() says <environment: R_GlobalEnv>"
[1] "sys.parent() says 1"
[1] "sys.frame(sys.parent()) says <environment: 0x2831dd0>"
[1] "sys.frame() says <environment: R_GlobalEnv>"
[1] "sys.parent() says 4"
[1] "sys.frame(sys.parent()) says <environment: 0x3b1dff8>"
[1] "sys.frame() says <environment: R_GlobalEnv>"
[1] "sys.parent() says 7"
[1] "sys.frame(sys.parent()) says <environment: 0x3b31c68>"
[1] "sys.frame() says <environment: R_GlobalEnv>"
[1] "sys.parent() says 10"
[1] "sys.frame(sys.parent()) says <environment: 0x3c238c0>"
Error in dive(level = 5, max.depth = 5) :
Not actually an error -- we're just tripping out!

这里发生了很多事情,但我会让我的问题保持简单:为什么 sys.parent() 首先增加 1(从 0 到 1),然后增加 3(即 1、4、7、10 .. .)?

最佳答案

那是因为您调用dive通过 eval .
下面的例子将清楚地表明:

> dive = function(level = 1, max.depth = 5)
+ {
+ m = match.call()
+ print(sys.calls())
+ cat("======================\n\n")
+ m[[2]] = m[[2]]+1
+ if(m[[2]] > max.depth)
+ stop("Not actually an error -- we're just tripping out!")
+ eval(m)
+ }
>
> dive(level = 1, max.depth = 3)
[[1]]
dive(level = 1, max.depth = 3)

======================

[[1]]
dive(level = 1, max.depth = 3)

[[2]]
eval(m)

[[3]]
eval(expr, envir, enclos)

[[4]]
dive(level = 2, max.depth = 3)

======================

[[1]]
dive(level = 1, max.depth = 3)

[[2]]
eval(m)

[[3]]
eval(expr, envir, enclos)

[[4]]
dive(level = 2, max.depth = 3)

[[5]]
eval(m)

[[6]]
eval(expr, envir, enclos)

[[7]]
dive(level = 3, max.depth = 3)

======================

Error in dive(level = 3, max.depth = 3) :
Not actually an error -- we're just tripping out!

这是修改后的版本,加一。
> dive = function(level = 1, max.depth = 5)
+ {
+ print(sys.calls())
+ cat("======================\n\n")
+ if(level+1 > max.depth)
+ stop("Not actually an error -- we're just tripping out!")
+ dive(level+1, max.depth)
+ }
>
> dive(level = 1, max.depth = 3)
[[1]]
dive(level = 1, max.depth = 3)

======================

[[1]]
dive(level = 1, max.depth = 3)

[[2]]
dive(level + 1, max.depth)

======================

[[1]]
dive(level = 1, max.depth = 3)

[[2]]
dive(level + 1, max.depth)

[[3]]
dive(level + 1, max.depth)

======================

Error in dive(level + 1, max.depth) :
Not actually an error -- we're just tripping out!

关于r - R 中的嵌套函数、sys.parent 和环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17182089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com