Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avoiding browser calls in R

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?

like image 502
Roman Luštrik Avatar asked Jun 17 '11 06:06

Roman Luštrik


People also ask

How do I disable browser in R?

The help page ? browser says that typing c and hitting enter will get you out of the browser and let the function continue to run or typing Q and hitting enter will exit the browser and the function and take you back to the top-level prompt.

What does the red dot in r mean?

R will treat the break point like a browser statement, going into browser mode when it encounters it. You can remove a break point by clicking on the red dot. The dot will disappear, and the break point will be removed.

What does a red dot mean in RStudio?

RStudio will also put a red dot next to the function name in the environment pane to indicate which of your functions currently have breakpoints. An advantage to using breakpoints is that they do not require saving or re-sourcing the function to take effect.

How to set a breakpoint in R?

The most common (and easiest) way to stop on a line of code is to set a breakpoint on that line. You can do this in RStudio by clicking to the left of the line number in the editor, or by pressing Shift+F9 with your cursor on the desired line. We call this an “editor breakpoint”.


2 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.

like image 75
G. Grothendieck Avatar answered Sep 21 '22 23:09

G. Grothendieck


Define global logical value

debug_mode <- TRUE

and then instead of browser() use

if (debug_mode) browser()
like image 20
Marek Avatar answered Sep 21 '22 23:09

Marek