When I'm in the debugger (from a browser
statement for ex.), if I find the code that gives an error, R exits the debugger. But I want to stay in it. How can I do that automatically (e.g. I don't want to manually have to remember to reset option(error) to something when I'm in the debugger.
Q to stop debug mode, terminate the function, and return to the R prompt.
8.3 Ignoring conditions The simplest way of handling conditions in R is to simply ignore them: Ignore errors with try() . Ignore warnings with suppressWarnings() . Ignore messages with suppressMessages() .
First is using options(warn=2) to make the R convert warnings to errors, and using traceback() to find out in which function the issue is arising. Often, this may be enough to get things back on track, especially if the function causing trouble is small.
You will actually need to first enter f press enter and then enter Q. The order is finish and then quit. Otherwise you might not leave the browsing.
You can use options(error = recover)
. This will exit the debugging session and immediately offer to enter a new one, with variables as they were at the time of the error.
For example,
options(error = recover)
myfun <- function(x) x + "a" ; debug(myfun) ; myfun(2)
This leads to the following interactive lines:
debugging in: myfun(2)
debug: x + "a"
Browse[2]> n
Error in x + "a" (from #1) : non-numeric argument to binary operator
Enter a frame number, or 0 to exit
1: myfun(2)
Selection: 1
Browse[3]> ls()
[1] "x"
Browse[3]> print(x)
[1] 2
Browse[3]>
To make this happen automatically, just put the options(error=recover)
call as a default for the session.
Your problem may be due to a misunderstanding about the levels of debug. If, for example, you execute debug(myfunc); myfunc(...)
, and myfunc
calls some other function, then the called function is not in debug mode. If that function throws an error, R
quite properly exits the entire environment. Imagine if it didn't: what would happen during non-debug mode?
One solution is: after entering myfunc
in debug mode, and you know what called function throws the error, to execute debug(that_func)
so you can follow its error path.
Another workaround is to manually enter the offending function call at the debug prompt (instead of hitting RETURN to have the debugger run the next line of your code). In this way, you'll get the error message back but since it was user-called rather than actually executing a line of the code being debug-run, the debugger will not exit.
Please feel free to comment if this is unclear.
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