2

I'm debugging R code with browser(). The function pauses the execution of the current R script and allows for inspection. Is it possible to enable/disable the debug mode on the fly, during the execution? With large scripts it would be very handy.

Thanks

1 Answer 1

9

Your question prompted me to read ?browser. The documentation says you can use the expr= argument to browser to create (the illusion) of conditional debugging. That, combined with a global option should give you what you want.

foo <- function(x) {
  browser(expr=isTRUE(getOption("myDebug")))
  mean(x)
}
foo(1:10)
options(myDebug=TRUE)
foo(1:10)  # invokes browser
4
  • +1 -- Thanks for reading browser for us all! I've always wrapped browser in if clauses for this purpose, but no longer. Nov 15, 2011 at 22:28
  • @JoshO'Brien: you may still want to wrap it in if statements... ?browser says that will be faster than using expr=. Nov 16, 2011 at 0:01
  • OK, that's it... Looks like I need to go read ?browser once and for all! Thanks. Nov 16, 2011 at 0:10
  • Super nifty trick. Added it to the debugging tricks compiled here stackoverflow.com/questions/1882734/… Nov 16, 2011 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.