Is there any reasonably simple, straightforward function along the line of getAnywhere() which returns the source code of a function with any comments, such that if I see no comments I can be confident that there are none, whether the code is in R, c, c++, Fortran, or something else? For example, stats:::plot.acf does not seem to have any comments in it. Can I conclude from this that there are no comments on its text?
I understand that there is a flowchart-like search process where if you know that source is written in R, then that source including comments is available from a specific github repository via some search method appropriate tp gethub. Also if you have determined that code is in some specified other language it is available via a more elaborate search process that involves finding the correct file and then doing text search within it, different for base and contributed packages. I am under the impression that at least until recently there was no shortcut to learning and working your way through that implicit flowchart search method if you wanted to learn whether there is a version of the code which contains comments. Moreover, I believe that that versions of the code which do or don't contain comments were nowhere identified as such, except by the comments themselves or by prior knowledge.
However, R is a pretty rapidly evolving ecosystem and I don't think it is entirely unreasonable to hope that simpler tools for determining whether there is a version of the source that includes comments, and finding it if there is, might now exist. Do they?
Whether the source code of an R function is preserved internally (via its
srcrefattribute) depends on the value of optionkeep.sourcewhen the function is defined. By source code, I mean the code as entered by the user, with comments, possibly inconsistent indentation, possibly inconsistent spacing around operators, etc.Whether functions in a contributed package retain their source code depends on options passed to
R CMD INSTALLwhen the package was built from sources (by you or by CRAN). The default is to discard source code, but you can avoid that by installing from sources and setting the--with-keep.sourceflag:Functions in base packages (
base,stats, etc.) won't have their source code unless you build R itself from sources with environment variableR_KEEP_PKG_SOURCEset toyes—at least, that is what I infer from?options. To learn about building R, see the corresponding manual.Given a function with source references, you can programmatically extract comments from the source code. A quick and dirty approach is pattern matching:
There can be false positives, though, because the pattern
#also matches strings and non-syntactic names containing the hash character, which aren't comments at all.A much more robust way to extract comments is to examine the parse data for tokens of type
COMMENT:Clearly,
getParseDatareturns much more information than you need. Here is a utility that you can use instead, which takes as an argument a function with source references and returns a character vector listing the comments, if any:AFAIK, there is no convenient mechanism for checking whether C code called by an R function contained comments before it was compiled...
Relevant documentation is a bit scattered, as always. I have found these help pages useful:
?parse,?deparse,?.deparseOpts,?srcref(and links therein),?options, and?getParseData.