Dasonk / docstring

Provides docstring like functionality to R functions without requiring the need to create a package.
57 stars 6 forks source link

Approach to avoid capture.output #26

Open cfhammill opened 4 years ago

cfhammill commented 4 years ago

Basically inconsequential suggestion, but I was curious how your package worked and I noticed the capture.output(print(fun)) line in read_docstring, https://github.com/Dasonk/docstring/blob/master/R/read_docstring.R#L17

I had guessed prior that it would be an application of body but that doesn't return the comments. So I dug around to figure out where print is finding the source code and I noticed it's in the function's attributes

as.character(attr(f, "srcref"))

This would avoid ever hitting the output stream. And would also work for function objects with overloaded print methods (not that those are very common).