如何引用函数R中的局部环境?

时间:2022-05-13 11:24:11

[This question has been resolved in the chat room, by Spacedman, but I'm posting it for others' benefit in the future.]

(这个问题已经在聊天室里由Spacedman解决了,但我现在把它贴出来是为了将来其他人的利益。)

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,它在其中创建localFunc。(NB:这不是一个包,而是在全球环境中。)我想知道localFunc在搜索路径中的位置,因为我想通过mvbutils: foodweb分析它。

Here is an example:

这是一个例子:

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)
}

However, a call to myFunc() does not seem to indicate that localFunc calls data.table(). This is incorrect - what gives?

但是,对myFunc()的调用似乎并不表示localFunc调用data.table()。这是不正确的-是什么原因?

(NB: The where argument specifies the search path.)

(NB: where参数指定搜索路径。)


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.)

更新1:正如Tommy和Spacedman指出的,诀窍是指定environment()。对foodweb()的调用指的是= c(1, ix)。索引1是错误的。这源于这样一种想法:. globalenv通常是search()向量中的第一项,是搜索的正确位置。这是错误的。相反,应该引用environment(),正确的调用如下。(NB: ix指定搜索()输出中的data.table()的位置。)

tmpWeb <- foodweb(where = c(environment(),ix), prune = vecPrune, plotting = FALSE)

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.

这个问题的答案出现在一个名为checkScriptDependencies的函数中,该函数将一个R脚本文件中的代码封装到一个本地函数中,然后由foodweb进行分析。这是一个关于如何使用环境()的有限示例,而Tommy已经给出了如何在此上下文中使用它和类似功能的良好解释。

1 个解决方案

#1


35  

To get the current environment, just call environment().

要获取当前环境,只需调用environment()。

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.

通常,sys.frame返回当前在调用堆栈上的任何环境,以及sys。nframe返回调用堆栈的当前深度。frame返回调用堆栈上所有环境的列表。

environment(f) returns the closure environment for a function f (where it will look for functions and global variables).

environment(f)返回函数f的闭包环境(在其中查找函数和全局变量)。

parent.env(e) returns the parent environment where it will look if a symbol is not found in e.

env(e)返回父环境,如果在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

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).

这将调用堆栈深度为3的本地函数floc。它返回一个包含当前环境的列表,它是父环境(f中的本地环境),它是父环境(其中定义了f,所以是globalenv)。它还返回堆栈帧(环境)的列表。这些是g中的递归调用的环境(最后一个是floc的当前环境)。

#1


35  

To get the current environment, just call environment().

要获取当前环境,只需调用environment()。

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.

通常,sys.frame返回当前在调用堆栈上的任何环境,以及sys。nframe返回调用堆栈的当前深度。frame返回调用堆栈上所有环境的列表。

environment(f) returns the closure environment for a function f (where it will look for functions and global variables).

environment(f)返回函数f的闭包环境(在其中查找函数和全局变量)。

parent.env(e) returns the parent environment where it will look if a symbol is not found in e.

env(e)返回父环境,如果在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

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).

这将调用堆栈深度为3的本地函数floc。它返回一个包含当前环境的列表,它是父环境(f中的本地环境),它是父环境(其中定义了f,所以是globalenv)。它还返回堆栈帧(环境)的列表。这些是g中的递归调用的环境(最后一个是floc的当前环境)。