views:

136

answers:

2

R is a functional programming language, and one of it's primary benefits is it's ability to create open and transparent functions.

As John Chambers says in his excellent book "Software for Data Analysis: Programming with R":

Computations are organized around functions, which can encapsulate specific, meaningful computational results, with implementations that can be examined for correctness.

Notions such as "reproducible research" and "trustworthy software" are at the heart of R development. In general, it is easy to examine a function just by typing its name without parenthesis. For instance:

> which
function (x, arr.ind = FALSE) 
{
    if (!is.logical(x)) 
        stop("argument to 'which' is not logical")
    wh <- seq_along(x)[x & !is.na(x)]
    dl <- dim(x)
...

My question is: how do you example the contents of functions such as for() or if() without downloading the R source code?

Edit: Incidentally, I understand that this won't help viewing compiled code (such as C, C++, or Java) that may be called from R. I'm really wondering if there is an R function which output's R functions.

+5  A: 

As the saying goes: Use the source, Luke!

R functions may be visible at the prompt, but are still parsed meaning that comments are stripped, code indentation is changed etc. So I would always go to the source.

Dirk Eddelbuettel
+2  A: 

I also like going to the source for the same reasons mentioned by Dirk, but for quick and dirty work I also rely on subsets of:

getAnywhere(), getFromNameSpace(), THEPACKAGE:::thefn, getGeneric()

to see functions which are not exported by the namespace of their package.

Jake