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?
If you have the . R file corresponding to the code you want to debug, it's easy to use editor breakpoints or browser() to add breakpoints to it. Sometimes, however, you don't have the source file for the code you want to debug. When this is the case, you can set a debug flag on the function you want to debug.
R code to make the changes I need? You can get the function code with getAnywhere(generic_RShiny) . To edit it, you could paste it into a R script file, make your edits, assign it to a new object (like my_generic_RShiny ) then run your new version of the function.
R provides a number of tools for debugging: traceback() debug() browser() trace() recover() 140.776 Statistical Computing R: Debugging Page 3 traceback() When an R function fails, an error is printed to the screen. Immediately after the error, you can call traceback() to see in which function the error occurred.
The built-in debugging tools (programs: functions) in R statistical computing environment are traceback(), debu(), browser(), trace(), and recover().
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With