Scott Stephens
2010-Feb-11 00:30 UTC
[R] Find Source File Corresponding to Sourced Function
I've noticed that when you debug a function that was loaded into the workspace using the source function, it prints the location of the file from which the function was sourced. Is there a way to get that same information without debugging the function? Thanks, Scott
Duncan Murdoch
2010-Feb-11 03:53 UTC
[R] Find Source File Corresponding to Sourced Function
On 10/02/2010 7:30 PM, Scott Stephens wrote:> I've noticed that when you debug a function that was loaded into the > workspace using the source function, it prints the location of the > file from which the function was sourced. Is there a way to get that > same information without debugging the function?Here's a function to do it. Probably we should have something like this in utils, but I've never got around to writing it. Put these into "test.R": ------------------------- f <- function(x) { x <- x^2 x } fnsrc <- function(f) { srcref <- attr(body(f), "srcref") if (is.null(srcref)) stop("No source reference") srcfile <- attr(srcref[[1]], "srcfile") startline <- srcref[[1]][1] paste(srcfile$filename, startline, sep="#") } ------------------------- Then: > fnsrc(f) [1] "test.R#1" > fnsrc(fnsrc) [1] "test.R#6" Duncan Murdoch
Apparently Analagous Threads
- file descriptor leak in getSrcLines in R 2.10.0 svn 48590
- Source references from the parser
- Why is srcref of length 6 and not 4 ?
- Proper way to drop 'srcref' from an expression created via substitute(function() ...)?
- formals(x)<- drops attributes including class