I need to extract the exact lines of the source that was parsed to create an R function, for use in coverage analysis. deparse
is not accurate enough because in doing coverage analysis with package covr
exact line numbers matter.
If there is a srcfile, I just need the filename. If there isn't, e.g. function was created in console, I need to create an equivalent temporary file that could have been, line by line, the source file for that function.
I see several function to extract src information from a function, like getsrcFilename
or getSrcref
, but none specifically to get the source code.
getSrclines
looked promising, but doesn't take functions as arguments. I tried to use attributes
to get to the srcref
and get to the information that way, but it doesn't seem to be stored consistently -- clearly I am missing something.
Sometimes
attributes(body(cover.fun))$srcfile
works and sometimes this attributes(attributes(cover.fun)$srcref)$srcfile)
does, and in the srcref
itself I found the source in srcfile$lines
or srcfile$original$lines
and of course these look just like experiments and not The Right Way to implement this.
I need something that takes care of functions created in a package, with source
or interactively. If the filename is available, that's all I need, otherwise I need the source lines. Thanks