As the doctor gone rogue

May 3, 2012

Getting help finding sourcecode in R

Filed under: Uncategorized — Tags: , — hypotheses @ 3:41 pm

(From:  page 43)

 It’s always a good idea to look under the hood and see how things work! Sometimes, that’s the only way to make sure that other people’s codes or programs work the way you really expect it to be, especially the free ones.


  1. The simplest case is typeing the function’s name without “()” following the function’s name

> matrix
function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
    if (is.object(data) || !is.atomic(data))
        data <- as.vector(data)
    .Internal(matrix(data, nrow, ncol, byrow, dimnames, missing(nrow),
<environment: namespace:base>

However, the comments are not included,  and it might still be better to really look at the sourcecode.
You can find the original sources after unpakcing the source package, in the directory “PackageName/R/”.
For R’s base pakcages, the R code is in$R_HOME/src/library/PakageName/R/.

  1. For codes hidden in a namespace,  type getAnywhere(“FunctionName”) to find out

> plot.factor
Error: object ‘plot.factor’ not found
> getAnywhere(plot.factor)
A single object matching ‘plot.factor’ was found
It was found in the following places
  registered S3 method for plot from namespace graphics
with value

function (x, y, legend.text = NULL, …)


<environment: namespace:graphics>

It is possible

to ask for available methods with methods(print).

The function of interest is the S3 method print.lm()

> methods(print)
  [1] print.acf*                              
  [2] print.anova                             
  [3] print.lm

A method hidden in a namespace

can be accessed (and therefore printed) directly

using the ::: operator as in stats:::print.lm.

For S4 related sources, it is advisable to look at the package’s source files directly!

  1. For “COMPILED” code sources, these are always more problematic. Why you type the name of these functions, what you will see are something like these: .C(), .Call(), .Fortran(), .External(), or .Internal(), or .Primitive().

– The first step is to look up the entry point in file ‘$R_HOME/src/main/names.c’, if the calling R function is either .Primitive() or .Internal().
You will find something like this
{“rnorm”,        do_random2,        8,        11,        3,        {PP_FUNCALL, PREC_FN,        0}},
This tells you to find “do_random2” in the source files.
– Then, grep ‘do_random2″ *.* in the source directory should point you to the correct file to look at. In this case, the source is in “random.c”


Now it’s time to learn “c” in order to understand more about these sources. At least, you will have to know the program structure well enough to be able to read other people’s codes.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: