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