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

Photo by NordWood Themes on Unsplash
Miscellaneous

How To Manage Multiple Screen Sessions

Linux’s Screen lets you run terminal applications to a Server in the background even if you disconnect from the ssh connection.

python exception
Python

Exceptions in Python

In this tutorial, we will provide you with an example of exception handling in Python. For simplicity, we will work