How can I view the source code for a function?
UseMethod("t")
is telling you that t()
is a (S3) generic function that has methods for different object classes.
The S3 method dispatch system
For S3 classes, you can use the methods
function to list the methods for a particular generic function or class.
> methods(t)
[1] t.data.frame t.default t.ts*
Non-visible functions are asterisked
> methods(class="ts")
[1] aggregate.ts as.data.frame.ts cbind.ts* cycle.ts*
[5] diffinv.ts* diff.ts kernapply.ts* lines.ts
[9] monthplot.ts* na.omit.ts* Ops.ts* plot.ts
[13] print.ts time.ts* [<-.ts* [.ts*
[17] t.ts* window<-.ts* window.ts*
Non-visible functions are asterisked
"Non-visible functions are asterisked" means the function is not exported from its package's namespace. You can still view its source code via the :::
function (i.e. stats:::t.ts
), or by using getAnywhere()
. getAnywhere()
is useful because you don't have to know which package the function came from.
> getAnywhere(t.ts)
A single object matching ‘t.ts’ was found
It was found in the following places
registered S3 method for t from namespace stats
namespace:stats
with value
function (x)
{
cl <- oldClass(x)
other <- !(cl %in% c("ts", "mts"))
class(x) <- if (any(other))
cl[other]
attr(x, "tsp") <- NULL
t(x)
}
<bytecode: 0x294e410>
<environment: namespace:stats>
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:
> library(Matrix)
Loading required package: lattice
> chol2inv
standardGeneric for "chol2inv" defined from package "base"
function (x, ...)
standardGeneric("chol2inv")
<bytecode: 0x000000000eafd790>
<environment: 0x000000000eb06f10>
Methods may be defined for arguments: x
Use showMethods("chol2inv") for currently available ones.
The output already offers a lot of information. standardGeneric
is an indicator of an S4 function. The method to see defined S4 methods is offered helpfully:
> showMethods(chol2inv)
Function: chol2inv (package base)
x="ANY"
x="CHMfactor"
x="denseMatrix"
x="diagonalMatrix"
x="dtrMatrix"
x="sparseMatrix"
getMethod
can be used to see the source code of one of the methods:
> getMethod("chol2inv", "diagonalMatrix")
Method Definition:
function (x, ...)
{
chk.s(...)
tcrossprod(solve(x))
}
<bytecode: 0x000000000ea2cc70>
<environment: namespace:Matrix>
Signatures:
x
target "diagonalMatrix"
defined "diagonalMatrix"
There are also methods with more complex signatures for each method, for example
require(raster)
showMethods(extract)
Function: extract (package raster)
x="Raster", y="data.frame"
x="Raster", y="Extent"
x="Raster", y="matrix"
x="Raster", y="SpatialLines"
x="Raster", y="SpatialPoints"
x="Raster", y="SpatialPolygons"
x="Raster", y="vector"
To see the source code for one of these methods the entire signature must be supplied, e.g.
getMethod("extract" , signature = c( x = "Raster" , y = "SpatialPolygons") )
It will not suffice to supply the partial signature
getMethod("extract",signature="SpatialPolygons")
#Error in getMethod("extract", signature = "SpatialPolygons") :
# No method found for function "extract" and signature SpatialPolygons
Functions that call unexported functions
In the case of ts.union
, .cbindts
and .makeNamesTs
are unexported functions from the stats
namespace. You can view the source code of unexported functions by using the :::
operator or getAnywhere
.
> stats:::.makeNamesTs
function (...)
{
l <- as.list(substitute(list(...)))[-1L]
nm <- names(l)
fixup <- if (is.null(nm))
seq_along(l)
else nm == ""
dep <- sapply(l[fixup], function(x) deparse(x)[1L])
if (is.null(nm))
return(dep)
if (any(fixup))
nm[fixup] <- dep
nm
}
<bytecode: 0x38140d0>
<environment: namespace:stats>
Functions that call compiled code
Note that "compiled" does not refer to byte-compiled R code as created by the compiler package. The <bytecode: 0x294e410>
line in the above output indicates that the function is byte-compiled, and you can still view the source from the R command line.
Functions that call .C
, .Call
, .Fortran
, .External
, .Internal
, or .Primitive
are calling entry points in compiled code, so you will have to look at sources of the compiled code if you want to fully understand the function. This GitHub mirror of the R source code is a decent place to start. The function pryr::show_c_source
can be a useful tool as it will take you directly to a GitHub page for .Internal
and .Primitive
calls. Packages may use .C
, .Call
, .Fortran
, and .External
; but not .Internal
or .Primitive
, because these are used to call functions built into the R interpreter.
Calls to some of the above functions may use an object instead of a character string to reference the compiled function. In those cases, the object is of class "NativeSymbolInfo"
, "RegisteredNativeSymbol"
, or "NativeSymbol"
; and printing the object yields useful information. For example, optim
calls .External2(C_optimhess, res$par, fn1, gr1, con)
(note that's C_optimhess
, not "C_optimhess"
). optim
is in the stats package, so you can type stats:::C_optimhess
to see information about the compiled function being called.
Compiled code in a package
If you want to view compiled code in a package, you will need to download/unpack the package source. The installed binaries are not sufficient. A package's source code is available from the same CRAN (or CRAN compatible) repository that the package was originally installed from. The download.packages()
function can get the package source for you.
download.packages(pkgs = "Matrix",
destdir = ".",
type = "source")
This will download the source version of the Matrix 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 and untared file. The uncompressing and untaring step can be done outside of R
, or from within R
using the untar()
function. It is possible to combine the download and expansion step into a single call (note that only one package at a time can be downloaded and unpacked in this way):
untar(download.packages(pkgs = "Matrix",
destdir = ".",
type = "source")[,2])
Alternatively, if the package development is hosted publicly (e.g. via GitHub, R-Forge, or RForge.net), you can probably browse the source code online.
Compiled code in a base package
Certain packages are considered "base" packages. These packages ship with R and their version is locked to the version of R. Examples include base
, compiler
, stats
, 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 tree in individual package directories under /src/library/
. How to access the R source is described in the next section.
Compiled code built into the R interpreter
If you want to view the code built-in to the R interpreter, you will need to download/unpack the R sources; or you can view the sources online via the R Subversion repository or Winston Chang's github mirror.
Uwe Ligges's R news article (PDF) (p. 43) is a good general reference of how to view the source code for .Internal
and .Primitive
functions. The basic steps are to first look for the function name in src/main/names.c
and then search for the "C-entry" name in the files in src/main/*
.
How to view the source code of an R function
Since it is a function within the vars
package, you can look at the source code the same way that you did for irf.varest
.
library(vars)
vars:::.irf
Output
function (x, impulse, response, y.names, n.ahead, ortho, cumulative)
{
if ((class(x) == "varest") || (class(x) == "vec2var")) {
if (ortho) {
irf <- Psi(x, nstep = n.ahead)
}
else {
irf <- Phi(x, nstep = n.ahead)
}
}
else if ((class(x) == "svarest") || (class(x) == "svecest")) {
irf <- Phi(x, nstep = n.ahead)
}
dimnames(irf) <- list(y.names, y.names, NULL)
idx <- length(impulse)
irs <- list()
for (i in 1:idx) {
irs[[i]] <- matrix(t(irf[response, impulse[i], 1:(n.ahead +
1)]), nrow = n.ahead + 1)
colnames(irs[[i]]) <- response
if (cumulative) {
if (length(response) > 1)
irs[[i]] <- apply(irs[[i]], 2, cumsum)
if (length(response) == 1) {
tmp <- matrix(cumsum(irs[[i]]))
colnames(tmp) <- response
irs[[i]] <- tmp
}
}
}
names(irs) <- impulse
result <- irs
return(result)
}
<bytecode: 0x7ff5bbb40138>
<environment: namespace:vars>
view source code in R
In response to
Non-visible functions are asterisked
, this means that the actual functions that are dispatched onts
or default objects, respectively, are in the tseries namespace but not exported. So just typetseries:::portfolio.optim.default
and you see the function code once you specify the full patch including the namespace.Whether R downloads source or a binary depends on your operating system. In either event, source for the tseries package is available. Reading source code written by experienced coders is a good way to learn.
How to view R source code
Try this:
getAnywhere(cooks.distance.glm)
From here: http://cran.r-project.org/doc/manuals/R-intro.html#Object-orientation
show source code for function in R
You have to ask using the corresponding method used by the function. Try this:
princomp # this is what you did without having a good enough answer
methods(princomp) # Next step, ask for the method: 'princomp.default'
getAnywhere('princomp.default') # this will show you the code
The code you are looking for is:
function (x, cor = FALSE, scores = TRUE, covmat = NULL, subset = rep(TRUE,
nrow(as.matrix(x))), ...)
{
cl <- match.call()
cl[[1L]] <- as.name("princomp")
if (!missing(x) && !missing(covmat))
warning("both 'x' and 'covmat' were supplied: 'x' will be ignored")
z <- if (!missing(x))
as.matrix(x)[subset, , drop = FALSE]
if (is.list(covmat)) {
if (any(is.na(match(c("cov", "n.obs"), names(covmat)))))
stop("'covmat' is not a valid covariance list")
cv <- covmat$cov
n.obs <- covmat$n.obs
cen <- covmat$center
}
else if (is.matrix(covmat)) {
cv <- covmat
n.obs <- NA
cen <- NULL
}
else if (is.null(covmat)) {
dn <- dim(z)
if (dn[1L] < dn[2L])
stop("'princomp' can only be used with more units than variables")
covmat <- cov.wt(z)
n.obs <- covmat$n.obs
cv <- covmat$cov * (1 - 1/n.obs)
cen <- covmat$center
}
else stop("'covmat' is of unknown type")
if (!is.numeric(cv))
stop("PCA applies only to numerical variables")
if (cor) {
sds <- sqrt(diag(cv))
if (any(sds == 0))
stop("cannot use cor=TRUE with a constant variable")
cv <- cv/(sds %o% sds)
}
edc <- eigen(cv, symmetric = TRUE)
ev <- edc$values
if (any(neg <- ev < 0)) {
if (any(ev[neg] < -9 * .Machine$double.eps * ev[1L]))
stop("covariance matrix is not non-negative definite")
else ev[neg] <- 0
}
cn <- paste("Comp.", 1L:ncol(cv), sep = "")
names(ev) <- cn
dimnames(edc$vectors) <- if (missing(x))
list(dimnames(cv)[[2L]], cn)
else list(dimnames(x)[[2L]], cn)
sdev <- sqrt(ev)
sc <- if (cor)
sds
else rep(1, ncol(cv))
names(sc) <- colnames(cv)
scr <- if (scores && !missing(x) && !is.null(cen))
scale(z, center = cen, scale = sc) %*% edc$vectors
if (is.null(cen))
cen <- rep(NA_real_, nrow(cv))
edc <- list(sdev = sdev, loadings = structure(edc$vectors,
class = "loadings"), center = cen, scale = sc, n.obs = n.obs,
scores = scr, call = cl)
class(edc) <- "princomp"
edc
}
<environment: namespace:stats>
I think this what you were asking for.
How can I read the source code for an R function?
What we see when you type summary
is
> summary
function (object, ...)
UseMethod("summary")
<bytecode: 0x0456f73c>
<environment: namespace:base>
This is telling us that summary is a generic function and has many methods attached to it. To see what those methods are actually called we can try
> methods(summary)
[1] summary.aov summary.aovlist summary.aspell*
[4] summary.connection summary.data.frame summary.Date
[7] summary.default summary.ecdf* summary.factor
[10] summary.glm summary.infl summary.lm
[13] summary.loess* summary.manova summary.matrix
[16] summary.mlm summary.nls* summary.packageStatus*
[19] summary.PDF_Dictionary* summary.PDF_Stream* summary.POSIXct
[22] summary.POSIXlt summary.ppr* summary.prcomp*
[25] summary.princomp* summary.srcfile summary.srcref
[28] summary.stepfun summary.stl* summary.table
[31] summary.tukeysmooth*
Non-visible functions are asterisked
Here we see all the methods associated with the summary
function. What this means is that there is different code for when you call summary on an lm object than there is when you call summary on a data.frame. This is good because we wouldn't expect the summary to be conducted the same way for those two objects.
To see the code that is run when you call summary on a data.frame you can just type
summary.data.frame
as shown in the methods list. You'll be able to examine it and study it and do whatever you want with the printed code. You mentioned that you were interested in factors so you will probably want to examine the output of summary.factor
. Now you might notice that some of the methods printed had an asterisk (*) next to them which implies that they're non-visible. This essentially means that you can't just type the name of the function to try to view the code.
> summary.prcomp
Error: object 'summary.prcomp' not found
However, if you're determined to see what the code actually is you can use the getAnywhere
function to view it.
> getAnywhere(summary.prcomp)
A single object matching ‘summary.prcomp’ was found
It was found in the following places
registered S3 method for summary from namespace stats
namespace:stats
with value
function (object, ...)
{
vars <- object$sdev^2
vars <- vars/sum(vars)
importance <- rbind(`Standard deviation` = object$sdev, `Proportion of Variance` = round(vars,
5), `Cumulative Proportion` = round(cumsum(vars), 5))
colnames(importance) <- colnames(object$rotation)
object$importance <- importance
class(object) <- "summary.prcomp"
object
}
<bytecode: 0x03e15d54>
<environment: namespace:stats>
Hopefully this helps you explore the code in R much more easily in the future.
For even more details you can view Volume 6/4 of The R Journal (warning, pdf) and read Uwe Ligge's "R Help Desk" section which deals with viewing the source code of R functions.
How to see the source code of R .Internal or .Primitive function?
The R source code of pnorm
is:
function (q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
.Call(C_pnorm, q, mean, sd, lower.tail, log.p)
So, technically speaking, typing "pnorm" does show you the source code. However, more usefully: The guts of pnorm
are coded in C, so the advice in the previous question view source code in R is only peripherally useful (most of it concentrates on functions hidden in namespaces etc.).
Uwe Ligges's article in R news, Accessing the Sources (p. 43), is a good general reference. From that document:
When looking at R source code, sometimes calls
to one of the following functions show up:.C()
,.Call()
,.Fortran()
,.External()
, or.Internal()
and.Primitive()
. These functions are calling entry points in compiled code such as shared objects,
static libraries or dynamic link libraries. Therefore,
it is necessary to look into the sources of the compiled code, if complete understanding of the code is
required.
...
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()
. This is done in the following example for the code implementing the ‘simple’ R functionsum()
.
(Emphasis added because the precise function you asked about (sum
) is covered in Ligges's article.)
Depending on how seriously you want to dig into the code, it may be worth downloading and
unpacking the source code as Ligges suggests (for example, then you can use command-line tools
such as grep
to search through the source code). For more casual inspection, you can view
the sources online via the R Subversion server or Winston Chang's github mirror (links here are specifically to src/nmath/pnorm.c
). (Guessing the right place to look, src/nmath/pnorm.c
, takes some familiarity with the structure of the R source code.)
mean
and sum
are both implemented in summary.c.
how to view c source code that has been loaded into R package
The C++ code of the function can be found in src/GWmodel.cpp
in the package's source files. The actual C++ function is also called gw_reg
but gets renamed at the point of export by Rcpp.
You can look at the function here on Github to see how it works in context, but the function itself is
List gw_reg(mat x, vec y, vec w, bool hatmatrix, int focus)
{
mat wspan(1, x.n_cols, fill::ones);
mat xtw = trans(x % (w * wspan));
mat xtwx = xtw * x;
mat xtwy = trans(x) * (w % y);
mat xtwx_inv = inv(xtwx);
vec beta = xtwx_inv * xtwy;
if (hatmatrix)
{
mat ci = xtwx_inv * xtw;
mat s_ri = x.row(focus - 1) * ci;
return List::create(
Named("beta") = beta,
Named("S_ri") = s_ri,
Named("Ci") = ci);
}
else
{
return List::create(
Named("beta") = beta);
}
}
I find the easiest way to track down such source code is to find the package on Github and use the search tool to look up the name of the C++ function.
How to view the source code of a NMF algorithm in R?
You can download the source here and then inspect the code for the function you are interested in. Additionally, try typing the name of the function at the console. If it is written in R you may see the whole function printed out. E.g.
f <- function(x) {return(x^2)}
f
function(x) {return(x^2)}
If the function is written in C/C++ it will give you information that it is compiled and you can look for the function in the downloaded source code, e.g.
head
function (x, ...)
UseMethod("head")
<bytecode: 0x10a0d9158>
<environment: namespace:utils>
Related Topics
Is R Superstitious Regarding Posixct Data Type
Jupyter-Client Has to Be Installed But "Jupyter Kernelspec --Version" Exited with Code 127
How to Create Textarea as Input in a Shiny Webapp in R
How to Replicate a Ddply Behavior That Uses a Custom Function with Dplyr
Convert Ggplot Object to Plotly in Shiny Application
Cbind Warnings:Row Names Were Found from a Short Variable and Have Been Discarded
"Long Vectors Not Supported Yet" Error in Rmd But Not in R Script
How to Make a Heatmap with a Large Matrix
R: How to Get the Last Element from Each Group
Relationship Between R Markdown, Knitr, Pandoc, and Bookdown
Error: --With-Readline=Yes (Default) and Headers/Libs Are Not Available
Differences in Heatmap/Clustering Defaults in R (Heatplot Versus Heatmap.2)
Plotting the Average Values for Each Level in Ggplot2
Grid Line Consistent with Ticks on Axis