18

I'm attempting to debug an R program, and I'm sorely missing the stop if error capability of matlab. I've gone through the R-debug-tools.pdf (mirror) documentation, but it didn't list anything like this. Is there any way to instruct R that, when any error at all is encountered - even one I didn't anticipate and plan for with a custom trace function - it should stop execution and throw me into browser() mode?

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
eykanal
  • 26,437
  • 19
  • 82
  • 113

2 Answers2

19

I believe you want options(error=recover)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
5

R has the wonderful stopifnot() which is pretty close to what C has in assert():

Description:

If any of the expressions in ‘...’ are not ‘all’ ‘TRUE’, ‘stop’ is called, producing an error message indicating the first of the elements of ‘...’ which were not true.

That is from help(stopifnot) which also has examples, as usual.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725