The article is about viewing the source code of R Method. There are different ways to view the source code of an R method or function. It will help to know how the function is working.
Table of Contents
Source Code of R Method (Internal Functions)
If you want to see the source code of R method or the internal function (functions from base packages), just type the name of the function at the R prompt such as;
rowMeans
Functions or Methods from the S3 Class System
For S3 classes, the methods
function can be used to list the methods for a particular generic function or class.
methods(predict)
Note that “Non-Visible functions are asterisked” means that the function is not exported from its package’s namespace.
One can still view its source code via the ::: function such as
stats:::predict.lm
or by using getAnywhere() function, such as
getAnywhere(predict.lm)
Note that the getAnywhere() function is useful as you don’t need to know from which package the function or method comes from.
Functions or Methods from the S4 Class System
The S4 system is a newer method dispatch system and is an alternative to the S3 system. The package ‘Matrix’ is an example of S4 function.
library(Matrix) chol2inv
The output already offers a lot of information. The standardGeneric is an indicator of an S4 function. The method to see defined S4 methods is to use showMethods(chol2inv), that is;
showMethods(chol2inv)
The getMethod can be used to see the source code of one of the methods, such as,
getMethod ("chol2inv", "diagonalMatrix")
View Source Code of Unexported Functions
In the case of unexported functions such as ts.union
, .cbindts
, and .makeNamesTs
from the stats namespace, one can view the source code of these unexported functions using the ::: operator or getAnywhere()
function, for example;
stats::: .makeNamesTs getAnywhere(.makeNamesTs)
Online MCQs Test Preparation Website