【发布时间】:2015-11-09 20:20:41
【问题描述】:
我正在一个看起来像的可变深度调用堆栈中工作
TopLevelFunction
-> <SomeOtherFunction(s), 1 or more>
-> AssignmentFunction
现在,我的目标是将AssignmentFunction 中创建的变量分配给TopLevelFunction 的环境。我知道我可以用sys.calls 提取堆栈,所以我目前的方法是
# get the call stack and search for TopLevelFunction
depth <- which(stringr::str_detect(as.character(sys.calls()), "TopLevelFunction"))
# assign in TopLevelFunction's environment
assign(varName, varValue, envir = sys.frame(depth))
我对此或多或少没意见,但我不确定将调用对象转换为字符向量是否是个好主意。这种方法容易出错吗?更一般地,您将如何搜索特定的父环境,只知道函数的名称?
【问题讨论】: