Debugging functions: browser traceback debug undebug
Browser
Since R is interactive it is usually very easy to debug your functions.
The main debugging tool in R is the function debug.
When the statement
browser()
is inserted in a function the function halts at that point and you can then
display the values of variables up to that point or enter:
n for execute next line and then halt | |
c continue execution until then end of the function | |
Q exit the function immediately |
Some variables names, like c, may collide with builtin R objects. In browser debug mode these variables are displayed by using the print function, print(c), rather than just entering c.
Debug
You can also enter browser mode immediately at the beginning of the
function with debug(func), where func
is the name of your function. Then when you run the func
it immediately enter browser mode starting at the line. To remove the
debugging flag: undebug(func)
Traceback
When a function terminates with an error entering traceback()
will show you which function generated that error. This is useful if there
are many nested functions.