\Header{sys.parent}{Functions to Access the Function Call Stack}
\alias{parent.frame}{sys.parent}
\alias{sys.call}{sys.parent}
\alias{sys.calls}{sys.parent}
\alias{sys.frame}{sys.parent}
\alias{sys.frames}{sys.parent}
\alias{sys.function}{sys.parent}
\alias{sys.nframe}{sys.parent}
\alias{sys.on.exit}{sys.parent}
\alias{sys.parents}{sys.parent}
\alias{sys.status}{sys.parent}
\keyword{programming}{sys.parent}
\keyword{data}{sys.parent}
\begin{Description}\relax
These functions provide access to \code{\Link{environment}}s (``frames''
in S terminology) associated with functions further up the calling stack.\end{Description}
\begin{Usage}
\begin{verbatim}
sys.call(which=<<see below>>)
sys.frame(which=<<see below>>)
sys.nframe()
sys.function(n=<<see below>>)
sys.parent(n=1)

sys.calls()
sys.frames()
sys.parents()
sys.on.exit()
sys.status()
parent.frame(n=1)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{which}] the frame number.
\item[\code{n}] the number of frame generations to go back.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{\Link{.GlobalEnv}} is given number 0 in the list of frames.
Each subsequent function evaluation increases the frame stack by 1
and the environment for evaluation of that function is returned by
\code{sys.frame} with the appropriate index.

The parent of a function evaluation is the environment in which the
function was called. It is not necessarily one less than the frame
number of the current evaluation, nor the environment from which it is
called.  \code{sys.parent} returns the number of the parent frame if
\code{n} is 1 (the default), the grandparent if \code{n} is 2, and so
forth. \code{sys.frame} returns the environment associated with a given
frame number.

\code{sys.call} and \code{sys.frame} both accept either positive or
negative values for the argument \code{which}.  Positive values of
\code{which} are normal frame numbers whereas negative values are counted
back from the frame number of the current evaluation.

Notice that even though the \code{sys.}\emph{xxx} functions are
interpreted, their contexts are not counted nor are they reported.
There is no access to them.

\code{sys.status()} returns a list with components \code{sys.calls},
\code{sys.parents} and \code{sys.frames}.

\code{sys.on.exit()} retrieves the expression stored for use by
\code{\Link{on.exit}} in the function currently being evaluated.
(Note that this differs from S, which returns a list of expressions
for the current frame and its parents.)

\code{parent.frame(n)} is a convenient shorthand for
\code{sys.frame(sys.parent(n))}.\end{Details}
\begin{SeeAlso}\relax
\code{\Link{eval}} for the usage of \code{sys.frame}.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
ff <- function(x) gg(x)
gg <- function(y) sys.status()
str(ff(1))

t1 <- function() {
  aa <- "here"
  t2 <- function() {
    ## in frame 2 here
    cat("current frame is", sys.nframe(), "\n")
    str(sys.calls()) ## list with two components t1() and t2()
    cat("parents are frame nos", sys.parents(), "\n") ## 0 1
    print(ls(envir=sys.frame(-1))) ## [1] "aa" "t2"
    invisible()
  }
  t2()
}
t1()

test.sys.on.exit <- function() {
  on.exit(print(1))
  ex <- sys.on.exit()
  str(ex)
  cat("exiting...\n")
}
test.sys.on.exit()
## gives `language print(1)', prints 1 on exit
\end{ExampleCode}
\end{Examples}

