Find the source file containing R function definition
Digging into the srcref
attribute of one of the loaded functions appears to work, if you go deep enough ...
source("tmp/tmpsrc.R")
str(util.add)
## function (a, b)
## - attr(*, "srcref")=Class 'srcref' atomic [1:8] 1 13 1 31 13 31 1 1
## .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x8fffb18>
srcfile <- attr(attr(util.add,"srcref"),"srcfile")
ls(srcfile)
## [1] "Enc" "filename" "fixedNewlines" "isFile"
## [5] "lines" "parseData" "timestamp" "wd"
srcfile$filename
## [1] "tmp/tmpsrc.R"
I know this was solved years ago, but I've just come across it and realised that there is a bit more to this if you use the body()
function.
The raw function has only the one attribute, "srcref"
which contains the code of the function, along with it's own attributes and class of "srcref"
(which dictates how it'll get printed).
The body()
of a function, such as body(util.add)
has three attributes.
"srcref"
which contains the body of the function stored as a list of expressions."srcfile"
which contains the source file of the function (which is what you are looking for in this question)"wholeSrcref"
which points to the entire source file.
This gives you an alternative (although slightly slower) method to extract the source file name attr(body(util.add),"srcfile")
, along with being able to see (although not interact with) the sibling functions (i.e. the other functions loaded in the same source file).
Not sure if it's useful, but it could be interesting.
Let's also not forget about the %@%
infix operator for accessing attributes using the {purrr}
package, with this we could use the more succinct (although again, slower) piece of code as:
util.add%@%srcref%@%srcfile