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 debug the issue?
-
1Possible duplicate of [r modify and rebuild package](https://stackoverflow.com/questions/34800331/r-modify-and-rebuild-package) – Scarabee Oct 13 '17 at 10:08
3 Answers
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.

- 9,317
- 3
- 29
- 38
-
hi ! I've downloaded a package and I know the function I have to edit - I just cannot find it among all the files. What is the right way to be doing this? Could you point me in the right direction? – vagabond Jul 12 '16 at 21:36
-
I think it's worth mentioning that once you have the edited source, you can simply assign it to a new function. This way it's made explicit in your code that you're using a modified version of the original package. – overdisperse Oct 09 '18 at 16:13
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

- 67,191
- 22
- 172
- 153
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.

- 360,940
- 56
- 644
- 725
-
1Ok, how do I do that? Normally I can just call 'library', which I'm assuming I wouldn't be able to do with the sources. I downloaded the source, but there are almost 40 files. Do I run 'source' on each of them individually to get them into my script? – Ben McCann Aug 02 '10 at 23:25
-
4@Ben McCann Assuming there's no C or FORTRAN code, you can extract R files, `source()` them and work from there (each time you change the file you must source it or copy/paste it into R). Once you've got everything working, you can copy the files back to the package binary and build it. I've described a quick step-by-step R package building in a blog post here: http://danganothererror.wordpress.com/2010/07/23/building-an-r-package-under-windows-without-c-c-or-fortran-code/ – Roman Luštrik Jun 07 '11 at 14:04