Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to ignore future browser calls

Tags:

r

debugging

When debugging, it is often useful to insert a browser() call in the code. If everything is fine and I would like to finish the function I can use c but if the browser is in a loop what would be the appropriate way to ignore this new calls?

I looked into browser help but didn't find something. I'm currently doing:

browser <- function(...){NULL}

Replacing the original browser function but it's not quite satifying.

like image 441
cmbarbu Avatar asked Dec 17 '25 11:12

cmbarbu


1 Answers

You can wrap browser in its own call and make it conditional:

breakpoint = function ()
    if (! exists('.break_disabled', parent.env(environment())))
        browser(skipCalls = 1L)

The skipCalls option is used because the browser call is nested inside a function rather than being called directly from where you want to break.

Now you just need to supply two functions to toggle breakpoints:

disable_break = function ()
    assign('.break_disabled', TRUE, parent.env(environment()))

enable_break = function ()
    if (exists('.break_disabled', parent.env(environment())))
        rm(.break_disabled, envir = parent.env(environment()))

It’s worth noting that just pasting the above functions into a script or an R session will pollute the global namespace with a hidden object. Normally I’d therefore define these functions in their own environment. However, for debugging purposes this should be fine.

like image 196
Konrad Rudolph Avatar answered Dec 20 '25 08:12

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!