R Source Code

WordcloudSource code access is one of the great benefits of R.  Source code is available for base R and over 5,000 open source packages.  There are many reasons to view source code: to know what software does when documentation is vague or incomplete; to combine code objects in custom scripts or libraries; and to change source code as needed.  The following post defines the different types of R source code available and how to access R sources.

Viewing Sources

The simplest way to view function source code is to type the function name (without parentheses) followed by enter:

This simple approach works well with R packages that have simple code structures. Unfortunately, the print results for many functions don’t return the actual source code, but instead return a call to the UseMethod command; or there’s a bit of source code available, but its obvious the real source lies elsewhere.  In the latter case, it is typical that print results display .External, .Internal, .Primitive, .C, .Fortran, etc. when a call to another script is made   For example:

Function objects with calls to additional sources can be followed and the process is relatively simple.

The S3 Method Dispatch System

Function objects which belong to the older S3 class are listed using the methods() command.   getAnywhere() will then display the source code for the named function.   For example:

The S4 Method Dispatch System

The S4 system is a newer method dispatch system and is an alternative to the S3 system. Here is an example of an S4 function:

The source code is not available, but the output offers a lot of information.  The phrase standardGeneric is an indicator of an S4 function. The command to list S4 functions is showMethods():

getMethod() can also be used to see the source code of one of the methods:

Syntax to view source code must also align to more complex signatures for each function method.  For example:

To see the source code for one of these methods the entire signature must be supplied.

Compiled Package Code

If you want to view compiled code in an R package, you will need to download and unpack the package source. A package’s source code is available from the same CRAN repository that the package was originally installed from. The download.packages() function can get the package source for you.

This will download the source version of the solaR package and save the corresponding .tar.gz file in the current directory. Source code for compiled functions can be found in the src directory of the uncompressed file. It is possible to combine the download and uncompress step into a single call (note that only one package at a time can be downloaded and unpacked in this way):

Alternatively, if the package development is hosted in a public archive (e.g. via GitHubR-Forge, or RForge.net), you can browse the source code online.

Compiled Code in R Base

Certain packages are considered “base” packages. These packages ship with R and their version is locked to the version of R. Examples include basecompilerstats, and utils. As such, they are not available as separate downloadable packages on CRAN as described above. Rather, they are part of the R source directory tree and the individual package directories can be found under /src/library/.

Compiled Code in the R Interpreter

If you want to view the code built-in to the R interpreter, you will need to download and unpack the R sources; or you can view the sources online via the R Subversion repository.

This entry was posted in R Packages, R Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + fifteen =