I have an elaborate script that spans multiple functions (and files). For debugging purposes I need to embed browser
calls into all sorts of nooks and crannies. When I presumably fix something, I want to run the whole thing without debugging, ergo avoiding browser
calls because commenting out all browser calls would mean a considerable effort from my part. @mdsumner on R chat suggested running the script in non-interactive mode (i.e. using Rscript.exe on Windows) but I would benefit from having that done in my console, to be able to access for instance traceback
. I have gone through browser docs and I can find no option that would come close to what I'm trying to achieve. Any suggestions?

- 69,533
- 24
- 154
- 197
-
1Have a look at the answers to this very similar question that was posted recently: http://stackoverflow.com/q/6286092/602276 – Andrie Jun 17 '11 at 08:14
-
I will use `options(debug = "FALSE")` and inside a function: `if (options("debug")) browser()`. Thanks! – Roman Luštrik Jun 17 '11 at 08:28
-
1wrap that inside a function definition called 'dbrowser' and replace all your browser() calls with dbrowser() - instead of replacing them with an if(options)-browser two-liner. – Spacedman Jun 17 '11 at 09:27
-
Good advice, @Spacedman. Care to write that down in a form of an answer so I can upvote you? – Roman Luštrik Jun 17 '11 at 09:58
-
@Spacedman Isn't this make `browser` to browse `dbrowser` function? – Marek Jun 17 '11 at 10:18
-
1Yes it will. And @Roman I think it should be `getOption("debug")` cause `options("debug")` returns `list` and is not interpreted as logical condition. – Marek Jun 17 '11 at 10:23
-
ouch yes. is there some way of telling browser to knock it up a level? I can't seem to get skipCalls to do what I think it should... Ah, it just changes the report, not the environment context... – Spacedman Jun 17 '11 at 14:59
-
Would sure love to hear what caused the downvote on this one. – Roman Luštrik Jun 18 '11 at 14:54
3 Answers
Here are three possibliities:
1) Overwrite browser command. Add this command to your global workspace to turn the browser commands off:
browser <- list
and to turn it back on
rm(browser)
This is probably the easiest but is a bit ugly due to the browser
variable being left in the global environment.
The next two solutions are slightly longer but use options instead so that no new variables are introduced into the global environment. Also they are such that if no options are set then no debugging is done so you only have to set an option if you want debugging. The if
solution may be faster than the expr
solution although its likely not material.
2) Use expr= argument with option. Replace each browser command with:
browser(expr = isTRUE(getOption("Debug")))
and then define the "Debug"
option to be TRUE
to turn debugging on.
options(Debug = TRUE)
or set it to something else or remove it to turn debugging off:
options(Debug = NULL)
3) Use if with an option. Replace each browser command with:
if (isTRUE(getOption("Debug"))) browser()
and then set the Debug
option or not as in the prior point.

- 254,981
- 17
- 203
- 341
-
All good comments from Michael and Marek, but I'm choosing this one as the correct one as it has an overview of three methods. – Roman Luštrik Jun 17 '11 at 14:44
Define global logical value
debug_mode <- TRUE
and then instead of browser()
use
if (debug_mode) browser()

- 49,472
- 15
- 99
- 121
I think this just comes down to nuanced use of a debugging function. If you want to selectively control the use of browser()
, put it inside an if
that lets you enable or disable debugging for the function. When you want browser to be called, make that explicit like
myfun(x, debug = TRUE)

- 29,099
- 6
- 83
- 91