Sys.frame, sys.nframe, etc. in R

Can someone please explain to me what exactly these various environment functions do? those. which one returns the frame? I am completely confused after reading the documentation (http://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'm trying to figure out how you can access the environments of the calling functions and know what environment you are in. For example, I g

could call f

:

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

      

If I wanted to get the content g

, how would I do it? What is the difference between frame and environment?

+3


source to share


1 answer


parent.frame()

refers to the calling environment. You usually don't need the rest. For your example, use this to list somevar

:



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

      

+2


source







All Articles