How to debug (placing break point,etc) an installed R package in RStudio?

I found a easy way to do this. First you write a script to recall the function, and then set a breakpoint. Run the script, and it you stop at the breakpoint. Then you can see there are different options to run the code. next line, step into the function, continue, stop...And now you can run the code line by line, and run into to your function.


  1. I think that when when you type install.packages('MultiPhen') you get a binary version of the package. I think that there is no way to set a breakpoint and step thru code with that version of the package.

  2. All R packages are open source, and the source is available on the CRAN page for the package. For example, this is the CRAN page for MultiPhen. If you click on the link next to the text "Package source:" you will download the source.

  3. In terms of what to do when you have the source: all R packages have the same directory structure. What matters for your situation is that all the R code for the package is in the directory called "R".

I recommend uninstalling the package from RStudio and sourcing the code in the directory "R", setting breakpoints and stepping thru code as you see fit.

Please let us know if this solves your problem.


Look at trace. Here is an example adding a breakpoint at the fourth statement in the base package function var. Here we ask trace to invoke the function browser at the sixth statement:

> trace(var, browser, at=6)
Tracing function "var" in package "stats"
[1] "var"
> var(1:10)
Tracing var(1:10) step 6 
Called from: eval(expr, envir, enclos)
Browse[1]> n
debug: if (is.data.frame(y)) y <- as.matrix(y) else stopifnot(is.atomic(y))
Browse[2]> n
debug: stopifnot(is.atomic(y))
Browse[2]> n
debug: .Call(C_cov, x, y, na.method, FALSE)
Browse[2]> n
[1] 9.166667

Remember to untrace when you're done. You can do fairly complex stuff with trace, though in most cases trace(fun.name, browser) is probably enough.

Alternatively, you can just load the package and type the name of the function on the command line like so:

> var
function (x, y = NULL, na.rm = FALSE, use) 
{
    if (missing(use)) 
        use <- if (na.rm) 
            "na.or.complete"
        else "everything"
    na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs", 
        "everything", "na.or.complete"))
    if (is.na(na.method)) 
        stop("invalid 'use' argument")
    if (is.data.frame(x)) 
        x <- as.matrix(x)
    else stopifnot(is.atomic(x))
    if (is.data.frame(y)) 
        y <- as.matrix(y)
    else stopifnot(is.atomic(y))
    .Call(C_cov, x, y, na.method, FALSE)
}
<bytecode: 0x000000000928ad30>
<environment: namespace:stats>

You can then copy that into your editor and play around with it, add your browser statement, and step through the results.