Wednesday, January 15, 2014

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

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 (is.data.frame(x)) 
        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]]
    z
}
<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)
object="ANY"
object="character"
    (inherited from: object="ANY")
object="DESeqDataSet"

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(is.na(pvalues) | 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>

Signatures:
        object        
target  "DESeqDataSet"
defined "DESeqDataSet"

Reference: http://stackoverflow.com/questions/5937832/r-show-source-code-of-an-s4-function-in-a-package

1 comment:

  1. Not to forget ::: for unexported function when you now the package name, i.e. stats:::predict.nls
    and the good old getAnywhere("profile.nls") which browses through the namespaces...

    Cheers,
    Andrej

    ReplyDelete