For example, in the code:
X <- environment()
F <- function() {
Y <- environment()
caller(Y)
}
F()
the environment called Y
was created by calling F()
, and that
call occurs in the environment called X
. In this case X
is the
calling environment of Y
, so F()
returns the same environment
as X()
.
caller
is intended as a replacement for parent.frame, which
returns the next environment up the calling stack -- which is
sometimes the same value, but differs in some cases such as when
lazy evaluation re-activates an environment. parent.frame()
can
return different things depending on the order in which arguments
are evaluated, and without warning. caller
will by default throw
an error if the caller cannot be determined.
In addition, caller
tries to do the right thing when the
environment was instantiated by means of do.call
, eval or
do rather than an ordinary function call.