Viewing Source Code for R Functions
R is a powerful programming language with many built-in features that can be overwhelming, especially when trying to understand how certain functions work. In this article, we will explore how to view the source code for various types of R functions.
S3 Method Dispatch System
In R, S3 is an object-oriented method dispatch system used by generic functions and classes. A generic function is a base function that can be extended with additional methods for different object classes. The UseMethod
function displays information about a generic function, including the name of the class it was called with.
To view the source code of a generic function, you can use the methods
function to list all methods available for a given generic function or class.
# View methods for t()
methods(t)
The output will show a list of all S3 methods that are available for the t()
function. The ^
symbol next to some method names indicates non-visible functions, which means they are not exported from their package’s namespace.
To view the source code of an S3 method, you can use the :::
operator or the getAnywhere
function.
# View source code for t()
stats:::.t
# View source code using getAnywhere
getAnywhere("stats::t")
S4 Method Dispatch System
S4 is a newer method dispatch system used by generic functions and classes. An S4 function has a single class parameter, whereas an S3 function can have multiple class parameters.
To view the methods available for an S4 generic function, you can use the showMethods
function.
# View methods for chol2inv()
showMethods(chol2inv)
The output will show all available methods for the chol2inv()
function. The method definition is also included in the output.
To view the source code of an S4 method, you can use the getMethod
function with the method name and signature specified.
# View source code for chol2inv() method
getMethod("chol2inv", "diagonalMatrix")
Functions that Call Unexported Functions
Some functions call unexported functions from the stats
namespace. You can view the source code of these functions using the :::
operator or the getAnywhere
function.
# View source code for makeNamesTs()
stats:::.makeNamesTs
Functions that Call Compiled Code
R also calls compiled C code through the .C
, .Call
, .Fortran
, .External
, and .Internal
functions. These functions are used by packages to interface with external libraries.
To view the source code of a function that calls compiled C code, you can use tools like pryr::show_c_source
.
# View source code using pryr::show_c_source
library(pryr)
pryr::show_c_source(".Internal")
Compiled Code in a Package
If you want to view the source code of a function that is built into a package, you will need to download/unpack the package source. The installed binaries are not sufficient.
To download the package source, you can use the download.packages
function.
# Download package source
download.packages(pkgs = "Matrix",
destdir = ".",
type = "source")
You can then extract the source code from the downloaded tarball using the untar
function or by manually extracting it.
Compiled Code in a Base Package
Some packages are considered “base” packages and ship with R. These packages are not available as separate downloadable packages on CRAN.
To view the source code of functions built into these base packages, you can download/unpack the R sources or view them online via the R Subversion repository or Winston Chang’s GitHub mirror.
Compiled Code Built into the R Interpreter
Finally, if you want to view the code built-in to the R interpreter itself, you will need to download/unpack the R sources or view them online.
Uwe Ligges’s R news article (PDF) provides a good general reference for viewing the source code of .Internal
and .Primitive
functions.
Last modified on 2023-11-06