## 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())) }
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'
this.path::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(this.path::sys.whiches(0)),
error = function(e) NULL)
)
}
try(force(fun2()))
Run the code above in your browser using DataLab