How to see source code of a function/method in R?

January 15, 2014

(This article was first published on One Tip Per Day, and kindly contributed to R-bloggers)

If it’s an internal function of R (e.g. from base package), just type the function name, like

> rowMeans
function (x, na.rm = FALSE, dims = 1L) 
    if ( 
        x <- as.matrix(x)
    if (!is.array(x) || length(dn <- dim(x)) < 2L) 
        stop(“‘x’ must be an array of at least two dimensions”)
    if (dims < 1L || dims > length(dn) – 1L) 
        stop(“invalid ‘dims'”)
    p <- prod(dn[-(1L:dims)])
    dn <- dn[1L:dims]
    z <- if (is.complex(x)) 
        .Internal(rowMeans(Re(x), prod(dn), p, na.rm)) + (0+1i) * 
            .Internal(rowMeans(Im(x), prod(dn), p, na.rm))
    else .Internal(rowMeans(x, prod(dn), p, na.rm))
    if (length(dn) > 1L) {
        dim(z) <- dn
        dimnames(z) <- dimnames(x)[1L:dims]
    else names(z) <- dimnames(x)[[1L]]
<bytecode: 0x111390028>

<environment: namespace:base>
If it’s a S4 function in a package, e.g. to see the code of plotMA in the DESeq2 package, first find the object it belongs to using showMethods(), like
> showMethods(‘plotMA’)
Function: plotMA (package BiocGenerics)
    (inherited from: object=”ANY”)
then to get source code by getMethod()
> getMethod(“plotMA”,”DESeqDataSet”)
Method Definition:

function (object, …) 
    .local <- function (object, lfcColname, pvalues, pvalCutoff = 0.1, 
        ylim, linecol = “#ff000080”, pointcol = c(“black”, “red”), 
        xlab, ylab, log = “x”, cex = 0.45, …) 
        if (missing(xlab)) 
            xlab <- “mean of normalized counts”
        if (missing(ylab)) 
            ylab <- expression(log[2] ~ fold ~ change)
        if (!missing(pvalues)) {
            if (length(pvalues) != nrow(object)) {
                stop(“length of pvalues should be equal to the number of rows of object”)
        stopifnot(length(pointcol) == 2)
        if (!”results” %in% mcols(mcols(object))$type) {
            stop(“first run DESeq() in order to produce an MA-plot”)
        if (missing(lfcColname)) {
            lfcColname <- lastCoefName(object)
        if (length(lfcColname) != 1 | !is.character(lfcColname)) {
            stop(“the argument ‘lfcColname’ should be a character vector of length 1”)
        if (missing(pvalues)) {
            res <- results(object, name = lfcColname)
            pvalues <- res$padj
        x <- mcols(object)
        stopifnot(length(cex) == 1)
        col <- ifelse( | pvalues > pvalCutoff, 
            pointcol[1], pointcol[2])
        col = col[x$baseMean > 0]
        x = x[x$baseMean > 0, ]
        py = x[, lfcColname]
        if (missing(ylim)) 
            ylim = c(-1, 1) * quantile(abs(py[is.finite(py)]), 
                probs = 0.99) * 1.1
        plot(x$baseMean, pmax(ylim[1], pmin(ylim[2], py)), log = log, 
            pch = ifelse(py < ylim[1], 6, ifelse(py > ylim[2], 
                2, 20)), cex = cex, col = col, xlab = xlab, ylab = ylab, 
            ylim = ylim, …)
        abline(h = 0, lwd = 4, col = linecol)
    .local(object, …)
<environment: namespace:DESeq2>

target  “DESeqDataSet”
defined “DESeqDataSet”


To leave a comment for the author, please follow the link and comment on their blog: One Tip Per Day. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

plotly webpage

dominolab webpage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)