Predictive Hacks

How to get the Source Code of a function in R

We can get the source code of an R function by running the getAnywhere() command. For example, if we want to get the source code of the rle built-in function we can run:

getAnywhere(rle)
A single object matching ‘rle’ was found
It was found in the following places
  package:base
  namespace:base
with value

function (x) 
{
    if (!is.vector(x) && !is.list(x)) 
        stop("'x' must be a vector of an atomic type")
    n <- length(x)
    if (n == 0L) 
        return(structure(list(lengths = integer(), values = x), 
            class = "rle"))
    y <- x[-1L] != x[-n]
    i <- c(which(y | is.na(y)), n)
    structure(list(lengths = diff(c(0L, i)), values = x[i]), 
        class = "rle")
}
<bytecode: 0x00000271304c55a0>
<environment: namespace:base>

For more information, you can have a look at the Stackoverflow discussion. Finally, we have written a post on how to get the source code of a function in Python.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore