When is / how / where is parent.frame in the default argument that is being interpreted?

Truth be told, I'm just being lazy here, but maybe someone will someday be able to profit from the answer here.

Let's say I define a function like:

fn<-function(envir=parent.frame())
{
    #do something with envir
}

      

My question is, what can I expect from the content of the envir?

Context: I had a fairly long function f1 that contained a call to parent.frame. Now I want to extract part of this function (containing the call to parent.frame) into a new helper function f2 (which will then be called f1), and I want to make sure that f1 does the same thing as before.

+3


source to share


1 answer


The default arguments are evaluated in the function call evaluation block from which place parent.frame()

is the calling environment. Thus, the value envir

will be a pointer to the environment from which it was called fn

.

Also try to see for yourself:



debug(fn)
fn()
# debugging in: fn()
# debug at #2: {
# }
Browse[2]> envir
# <environment: R_GlobalEnv>

      

+3


source







All Articles