[R] code of an R function {"probably easy methods question"}
Martin Maechler
maechler at stat.math.ethz.ch
Wed Sep 10 11:27:47 CEST 2008
>>>>> "DScottNZ" == David Scott <d.scott at auckland.ac.nz>
>>>>> on Wed, 10 Sep 2008 11:29:31 +1200 (NZST) writes:
DScottNZ> On Tue, 9 Sep 2008, Edna Bell wrote:
>> Dear R Gurus:
>> I want to look at the code for the t.test function. I did the following:
>>
>>> t.test
>> function (x, ...)
>> UseMethod("t.test")
>> <environment: namespace:stats>
>>> getAnywhere("t.test")
>> A single object matching 't.test' was found
>> It was found in the following places
>> package:stats
>> registered S3 method for t from namespace stats
>> namespace:stats
>> with value
>>
>> function (x, ...)
>> UseMethod("t.test")
>> <environment: namespace:stats>
>>> methods("t.test")
>> [1] t.test.default* t.test.formula*
>>
>> Non-visible functions are asterisked
>>>
>>
>> Ok. Now what, please? I'm sure it's really simple.
DScottNZ> getAnywhere(t.test.default)
yes, or in this case, stats:::t.test.default
*HOWEVER* this does *NOT* give you "the code for" the t.test()
function in the strict sense.
It gives you the (print() of the) parsed and deparsed version of
the code. The source code typically also has *comments* and may
have been (and typically is - with "standard R" code) written
indented and formatted *better* than what you print() does.
To see the source code you must look at the *source*,
i.e., into the source file, typically <foo>.R in directory <pkg>/R/
For standard R, the source is not only packed in the
R-<version>.tar.gz file you can get from CRAN, but also
available via subversion and (less powerfully) via a web
browser, pointing to https://svn.R-project.org/R/
E.g. for the R 2.7.2 version of t.test
https://svn.r-project.org/R/tags/R-2-7-2/src/library/stats/R/t.test.R
or for the "always latest R-devel" version -- currently
identical to the 2.7.2 one:
https://svn.r-project.org/R/trunk/src/library/stats/R/t.test.R
Note that most people would want to *search* in the R sources,
and therefore still get the complete R sources (either by
subversion or from a *.tar.gz file, from CRAN).
Yes, Uwe Ligges wrote an R News (2006 (4), p.43-45)
article on the theme...
See R homepage -> "Newsletter", or directly (currently)
http://www.r-project.org/doc/Rnews/
Martin Maechler, ETH Zurich
More information about the R-help
mailing list