开发者

How to edit and debug R library sources

I've included a library called blotter in my R script which has a bug in it. Is there an easy way for me to edit the source to try and deb开发者_如何学编程ug the issue?


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.


Such a feature is implemented in the development version of R (Jul 16, 2010):

A new facility has been added to r-devel for experimenting by authors of packages.

The idea is to insert modified code from the package source into the running package without re-installing. So one can change, test, change, etc in a quick loop.

The mechanism is to evaluate some files of source code, returning an environment object which is a snapshot of the code. From this environment, functions and methods can be inserted into the environment of the package in the current session. The insertion uses the trace() mechanism, so the original code can be restored.

The one-step version is:

insertSource("mySourceFile.R", package = "myPackage", functions = "foo")

See this post for further details: Inserting and testing revised functions in a package


Your question of Is there an easy way for me to edit the source to try and debug the issue? has the obvious answer: Use the source, Luke!

blotter is a package on R-Forge from where you can get blotter sources here. That is the standard way of looking at Open Source and possibly helping it along with a bug fix.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜