Learn R Programming

this.path (version 2.2.0)

sys.srcref: Get 'srcref' From Call Stack

Description

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

Usage

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

Value

A srcref object or NULL.

Arguments

n

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

which

the frame number to inspect for source references. An alternative to specifying n.

Examples

Run this code
## 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()` = this.path::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())) }

Run the code above in your browser using DataLab