如何在R中引用函数中的局部环境? [英] How can I reference the local environment within a function, in R?
问题描述
[此问题已由Spacedman在聊天室中解决,但我要发布以便将来为他人谋取利益.]
[This question has been resolved in the chat room, by Spacedman, but I'm posting it for others' benefit in the future.]
我有一个函数myFunc
,可在其中创建localFunc
. (注意:这不是在程序包中,而是在全局环境中.)我想知道localFunc
在搜索路径中的位置,因为我想通过mvbutils::foodweb
对其进行分析.
I have a function, myFunc
, which creates localFunc
inside of it. (NB: this is not in a package, but in the global environment.) I'd like to know where localFunc
exists in the search path, as I'd like to analyze it via mvbutils::foodweb
.
这里是一个例子:
myFunc <- function(){
require(data.table)
require(mvbutils)
localFunc <- function(x){
return(as.data.table(x))
}
vecPrune <- c("localFunc",ls("package:data.table"))
ix <- match("data.table",search())
tmpWeb <- foodweb(where = c(1,ix), prune = vecPrune, plotting = FALSE)
return(tmpWeb)
}
但是,对myFunc()
的调用似乎并不表示localFunc
会调用data.table()
.这是不正确的-有什么作用?
However, a call to myFunc()
does not seem to indicate that localFunc
calls data.table()
. This is incorrect - what gives?
(注意:where
参数指定搜索路径.)
(NB: The where
argument specifies the search path.)
更新1:正如Tommy和Spacedman指出的那样,诀窍是指定environment()
.对foodweb()
的调用指的是where = c(1, ix)
.索引1
是一个错误.这是由于认为.GlobalEnv
(通常是(总是?)search()
向量中的第一项)是正确的搜索位置而引起的.那是错误的.相反,应该参考environment()
,下面是正确的调用. (注意:ix
指定data.table()
在search()
输出中的位置.)
Update 1: As Tommy and Spacedman point out, the trick is to specify environment()
. The call to foodweb()
refers to where = c(1, ix)
. The index 1
is a mistake. That arose from thinking that .GlobalEnv
, which is often (always?) the first item in the search()
vector, is the right place to search. That is erroneous. Instead, one should refer to environment()
, and the correct call is below. (NB: ix
specifies the location of data.table()
in the search()
output.)
tmpWeb <- foodweb(where = c(environment(),ix), prune = vecPrune, plotting = FALSE)
此问题出现在checkScriptDependencies
函数的此问题的答案中,该函数将代码从将R脚本文件转换为本地函数,然后由foodweb
分析.这是一个如何使用environment()
的有限示例,Tommy已很好地说明了如何使用environment()
和类似的功能.
This appears in the answer to this question, in a function called checkScriptDependencies
, which wraps the code from an R script file into a local function, which is then analyzed by foodweb
. This is a limited example of how to use environment()
, and Tommy has given a good explanation of how to use it and similar functions in this context.
推荐答案
要获取当前环境,只需调用environment()
.
To get the current environment, just call environment()
.
通常,sys.frame
返回当前在调用堆栈上的任何环境,而sys.nframe
返回当前在调用堆栈上的深度. sys.frames
返回调用堆栈上所有环境的列表.
In general, sys.frame
returns any of the environments currently on the call stack, and sys.nframe
returns the current depth of the call stack. sys.frames
returns a list of all environments on the call stack.
environment(f)
返回函数f
的关闭环境(它将在其中查找函数和全局变量).
environment(f)
returns the closure environment for a function f
(where it will look for functions and global variables).
parent.env(e)
返回父环境,如果在e
中未找到符号,它将查找该父环境.
parent.env(e)
returns the parent environment where it will look if a symbol is not found in e
.
f <- function() {
function() list(curEnv=environment(), parent=parent.env(environment()),
grandParent=parent.env(parent.env(environment())), callStack=sys.frames(),
callStackDepth=sys.nframe())
}
g <- function(f, n=2) if (n>2) g(f, n-1) else f()
floc <- f() # generate a local function
g(floc, 3) # call it
这将调用堆栈深度为3的本地函数floc
.它返回一个列表,其中包含当前环境,其父级(f
中的本地环境)和其父级父级(其中f
为定义,因此globalenv
).它还返回堆栈框架(环境)的列表.这些是g
中的递归调用的环境(最后一个除外,这是floc
的当前环境).
This will call the local function floc
with a stack depth of 3. It returns a list with the current environment, it's parent (the local environment in f
), and it's grand parent (where f
was defined, so globalenv
). It also returns the list of stack frames (environments). These are the environments for the recursive calls in g
(except the last one which is the current environment of floc
).
这篇关于如何在R中引用函数中的局部环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!