sys: Call Stack Inspection

sysR Documentation

Call Stack Inspection

Description

sys.srcref() is the R-level version of the function that implements this.path(). It retrieves the srcref of the call n generations back from itself.

sys.whiches() is the R-level version of the function that implements sys.srcref(). It returns the frame numbers of all calls with a common parent frame n generations back from itself.

Usage

sys.srcref(n = 1, which = if (n) sys.parent(n) else 0)
sys.whiches(n = 1, which = if (n) sys.parent(n) else 0)

Arguments

n

See ?this.path(). However, note that n defaults to 0 in this.path() but defaults to 1 here.

which

frame number to inspect. An alternative to specifying n.

Value

for sys.srcref(), a srcref object or NULL.

for sys.whiches(), an integer vector.

Examples

## this example will not work with 'Run examples'
## which uses 'package:knitr' since knitted
## documents do not store source references
fun <- function ()
{
    list(
        `sys.srcref()` = @R_PACKAGE_NAME@::sys.srcref(),
        ## while this might seem like a simpler alternative,
        ## you will see it does not work in a couple cases below
        `attr(sys.call(sys.parent()), "srcref")` =
            attr(sys.call(sys.parent()), "srcref")
    )
}


## the braces are unnecessary when using example("sys.srcref"),
## but are needed when copied into the R Console
{ fun() }
{ print(fun()) }
{ try(print(fun())) }





fun2 <- function ()
{
    list(
        ## if copied directly into the R Console,
        ## should be 1 6 7
        ## 1 is the frame number associated with 'try'
        ## 6                                     'force'
        ## 7                                     'fun2'
        @R_PACKAGE_NAME@::sys.whiches(),


        ## if copied directly into the R Console,
        ## should be  8 12 13
        ##  8 is the frame number associated with 'tryCatch'
        ## 12                                     'identity'
        ## 13                                     'sys.whiches'
        tryCatch(identity(@R_PACKAGE_NAME@::sys.whiches(0)),
            error = function(e) NULL)
    )
}


try(force(fun2()))



ArcadeAntics/this.path documentation built on July 27, 2024, 12:05 a.m.