How to Edit and Debug R Library Sources

How to edit and debug R library sources

Look up the trace and browser functions, they are the basic tools in R for debugging. Say you want to edit the source of function foo, then saying

trace("foo",edit=TRUE)

will open up the source of foo in the editor for you to change. However, this is for interactive debugging; the source files in the packages are not changed. So once you have found the bug, you need to change the package source files for the change to be permanent.

How to edit and debug R library sources

Look up the trace and browser functions, they are the basic tools in R for debugging. Say you want to edit the source of function foo, then saying

trace("foo",edit=TRUE)

will open up the source of foo in the editor for you to change. However, this is for interactive debugging; the source files in the packages are not changed. So once you have found the bug, you need to change the package source files for the change to be permanent.

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

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.

r modify and rebuild package


Linux environment

Starting with downloading the package source from CRAN.

  • This is the landing page: https://cran.r-project.org/web/packages/SemiMarkov/index.html
  • This is the package source: https://cran.r-project.org/src/contrib/SemiMarkov_1.4.2.tar.gz

Download and extract the source:

wget https://cran.r-project.org/src/contrib/SemiMarkov_1.4.2.tar.gz
tar -xvzf SemiMarkov_1.4.2.tar.gz

This should result in a directory named SemiMarkov. Open up the source (cd SemiMarkov), and modify as necessary.

Next, build the changes:

cd ..
R CMD build SemiMarkov/

This will result in a new archive file named SemiMarkov_1.4.2.tar.gz.

Lastly, install your modified archive:

R CMD INSTALL SemiMarkov_1.4.2.tar.gz

Windows environment

I'm less familiar with the Windows platform. *nix tooling is available in Cygwin, but it's painful. Instead, as Josh O'Brien points out, you should follow the Windows-specific instructions in the R Installation and Administration manual.

Debug location is approximate because the source is not available in R 4.0.0 + RStudio

I investigated this using your package and discovered that what you're seeing here is a bug fairly deep in the RStudio / R interface, caused by some subtle changes R made in R 4.0 around source references.

The problem happens whenever there's a backslash (\) in a function's code. When that happens, R is escaping it, which causes RStudio to think that the copy of the function you're looking at is different than the one in the file, which in turn causes it to show you a copy in a code browser instead of opening the file itself.

Since your csem() function contains a backslash it triggers the issue. I've written this up on our issue tracker here:

https://github.com/rstudio/rstudio/issues/6854

Using trace on a hidden function to edit source code in R

Thats not working because the hidden function .toFF is not known in the global env because it is hidden. If you want to edit/debug hidden function you have to specify the where argument in trace() with the corresponding function. In your case it would be RLBigDataLinkage.

Regarding to the docs of trace, it is stated:

For “hidden” functions such as S3 methods in a namespace, where = *
typically needs to be specified as well

So for your answer this will work:

trace(".toFF", edit=T, where = RLBigDataLinkage)


Related Topics



Leave a reply



Submit