[R] how can i locate the source code of a module quickly?

Duncan Murdoch murdoch at stats.uwo.ca
Tue Jan 17 15:01:47 CET 2006


On 1/17/2006 8:33 AM, obeeker at gmail.com wrote:
> I have  dowloaded the Source Code of R,and I want to know the process of
> chi-sqared test,but how can I found it?

Which function are you using?  Let's assume chisq.test.  Then the first 
thing to do is to type the function name:

 > chisq.test
function (x, y = NULL, correct = TRUE, p = rep(1/length(x), length(x)),
     rescale.p = FALSE, simulate.p.value = FALSE, B = 2000)
{
     DNAME <- deparse(substitute(x))

[ lots of skipped lines ]

     structure(list(statistic = STATISTIC, parameter = PARAMETER,
         p.value = PVAL, method = METHOD, data.name = DNAME, observed = x,
         expected = E, residuals = (x - E)/sqrt(E)), class = "htest")
}
<environment: namespace:stats>
 >

This is a deparsed version of the source; often it's good enough.  If 
not, it tells you that the function is in the stats package, so you can 
look for the original source (with comments, if you're lucky) in 
src/library/stats/R.

For some functions you get less information:

 > pchisq
function (q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)
{
     if (missing(ncp))
         .Internal(pchisq(q, df, lower.tail, log.p))
     else .Internal(pnchisq(q, df, ncp, lower.tail, log.p))
}
<environment: namespace:stats>

The .Internal() call tells you that this function is mostly implemented 
in C or Fortran code from the R source.  (.C() or .Fortran() or .Call() 
would tell you that the source is in the package src/library/stats/src 
directory).

To find its source, look in src/main/names.c.  There's a huge table 
there, containing these lines:

{"dchisq",	do_math2,	6,	11,	2+1,	{PP_FUNCALL, PREC_FN,	0}},
{"pchisq",	do_math2,	7,	11,	2+2,	{PP_FUNCALL, PREC_FN,	0}},
{"qchisq",	do_math2,	8,	11,	2+2,	{PP_FUNCALL, PREC_FN,	0}},

These tell you that the chisq functions are all implemented in a 
function called do_math2, with codes 6, 7, and 8 respectively.  (Further 
up in names.c is a comment describing the other columns.)

You need to do a grep or other search to find do_math2; it's in 
src/main/arithmetic.c.  You can follow the source from there.

Sometimes the source to the function won't tell you which package it is 
in (because the package doesn't have a namespace defined).  In that case 
the "getAnywhere" function can be useful, e.g.

 > getAnywhere("airmiles")
A single object matching 'airmiles' was found
It was found in the following places
   package:datasets
with value

Time Series:
Start = 1937
End = 1960
Frequency = 1
  [1]   412   480   683  1052  1385  1418  1634  2178  3362  5948  6109 
  5981
[13]  6753  8003 10566 12528 14760 16769 19819 22362 25340 25343 29269 30514

airmiles isn't a function, it's a dataset, and it is located in 
src/library/datasets/data/airmiles.R.

I hope this helps.

Duncan Murdoch




More information about the R-help mailing list