R中的sys.frame,sys.nframe等

时间:2022-05-15 06:08:17

could someone please explain to me what these various environment functions do specifically? ie which one returns what frame? i am thoroughly confused after reading the documentation (http://stat.ethz.ch/R-manual/R-patched/library/base/html/sys.parent.html)

有人可以向我解释这些不同的环境功能具体做什么?即哪一个返回什么帧?阅读文档后我感到非常困惑(h​​ttp://stat.ethz.ch/R-manual/R-patched/library/base/html/sys.parent.html)

Let's put some structure on the question:

让我们在问题上加上一些结构:

x = 1; y=2; z=3;
f = function() { ls(); ls(envir=sys.frame());}
#this first prints the contents of this function and then of the global environment

I am trying to understand how one can access environments of calling functions and to know which environment you are in. For example g could have called f:

我试图了解如何访问调用函数的环境以及了解您所处的环境。例如,g可以调用f:

g = function() { somevar=1; f() }

If I wanted to get the contents of g, how would i do that? What is the difference between a frame and an environment?

如果我想获得g的内容,我该怎么做?框架和环境有什么区别?

1 个解决方案

#1


2  

parent.frame() refers to the calling environment. You normally don't need the rest of them. For your example use this to list somevar :

parent.frame()指的是调用环境。你通常不需要其余的。对于您的示例,使用此列出somevar:

f <- function() ls(parent.frame())

#1


2  

parent.frame() refers to the calling environment. You normally don't need the rest of them. For your example use this to list somevar :

parent.frame()指的是调用环境。你通常不需要其余的。对于您的示例,使用此列出somevar:

f <- function() ls(parent.frame())