0

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

Alex
  • 6,228
  • 1
  • 22
  • 18
piccolbo
  • 1,305
  • 7
  • 17
  • So is this really a question about `covr`? Or did you just want to see [the source code for a function](http://stackoverflow.com/questions/19226816/how-can-i-view-the-source-code-for-a-function)? How is `deparse()` not accurate enough? It would help to give more specific examples. – MrFlick Apr 14 '15 at 23:43
  • No, covr was only mentioned to say that this comes from a real application, not some silly curiosity. `deparse` doesn't preserve line breaks. – piccolbo Apr 15 '15 at 06:07

0 Answers0