r - 动态地将控制移动到调用堆栈中的其他位置
问题描述
在这个用例中,我想动态地将控制移动到调用堆栈中的其他位置。这可能吗?下面,如果我们修改并不理会身体,我们可以top_level()
返回 2吗?return_2_from_top_level()
top_level()
# Do not modify this:
top_level <- function() {
return_2_from_top_level()
Sys.sleep(2)
1
}
# Modify this:
return_2_from_top_level <- function() {
# What can we do here to make top_level() return 2?
evalq(return(2), sys.frame(1)) # Does not work, but kind of shows the idea.
}
# We want 2, not 1:
top_level()
#> [1] 1
由reprex 包于 2020-01-11 创建(v0.3.0)
期望的结果:
top_level()
#> [1] 2
# We still want this function body for top_level():
body(top_level)
#> {
#> return_2_from_top_level()
#> Sys.sleep(2)
#> 1
#> }
解决方案
可能您可以首先修改整个设置以避免这种情况,但如果没有,则callCC
可以使用。
1)重要的是return_2_from_top_level
必须知道,g
因此我们将指示的功能环境重置为已知的top
环境。g
return_2_from_top_level <- function() g(2)
top_level <- function() {
return_2_from_top_level()
Sys.sleep(2)
1
}
top <- function(g) {
environment(return_2_from_top_level) <- environment()
environment(top_level) <- environment()
top_level()
}
callCC(top)
## [1] 2
2)或者,我们可以将 和 的定义top_level
移入
return_2__from_top_level
,top
在这种情况下,没有明确的环境混乱。
top <- function(g) {
return_2_from_top_level <- function() g(2)
top_level <- function() {
return_2_from_top_level()
Sys.sleep(2)
1
}
top_level()
}
callCC(top)
## [1] 2
3)另一种可能性是复制g
到全局环境中。
return_2_from_top_level <- function() g(2)
top_level <- function() {
return_2_from_top_level()
Sys.sleep(2)
1
}
top <- function(g) {
g <<- g
top_level()
}
callCC(top)
## [1] 2
推荐阅读
- docker - 有什么方法可以检查 mosquitto 是否确实在使用我的自定义 mosquitto.conf 文件而不是默认文件?
- java - Java:如何打印 Map 元素的字符串值?
- html - 滑块下一个/上一个。按钮
- javascript - 如何在手动添加的字段中插入值而脚本 imgur 不生成另一个?
- react-native - 反应原生平面列表改变方向
- c++ - Intel NCS2 vpu 不支持动态批处理
- c# - 替换字符串句子中的多个单词C#
- elasticsearch - Filebeat hassting 问题
- python - 如何从开关访问开关参数/ ID?
- laravel - 在 laravel 中同时以用户和管理员身份登录