I personally marked this question as a duplicate since this question appears to be nearly identical to what @TylerRinker's. However, I would still consider this specific question unanswered. While I like debug/debugonce
those are different (albeit very useful) tools and I remain curious as to how to control browser
contexts.
When debugging, it's easy and often helpful to step into a browser
context from within another browser
context. When the nested level is called from an iterative loop, exiting back to the previous context requires stepping through each iteration.
Quitting via Q
exits from all browser
contexts.
Is there a way to specify which context to exit to without needing to manually proceed through all iterations at the lower context?
In the example below, that would mean going back to Browse [1]
without having to hit c
100 times
The two I use are either to
(1) modify the number of iterations or
(2) use Q
, call my original function again, then walk forward to where I previously was.
Neither constitutes "great" or event "good."
Example:
outerFunc <- function() {
# < ... cropped .. >
browser()
# < ... cropped .. >
}
outerFunc()
lapply(seq(100), function(i) browser(text=paste("i is #", i)))
> outerFunc()
Called from: outerFunc()
Browse[1]> lapply(seq(100), browser)
Called from: lapply(seq(100), browser)
Browse[2]> c
Called from: lapply(seq(100), browser)
Browse[2]> c
Called from: lapply(seq(100), browser)
Browse[2]> c
Called from: lapply(seq(100), browser)
Browse[2]> Q # <~~~~~~ `Q` quits all contexts
>
Here is one maybe slightly hacky idea that takes advantage of browser()
's expr=
argument to allow you to turn off browsing within the loop at any time.
outerFunc <- function() {
# < ... cropped .. >
BROWSE <- TRUE
browser()
# < ... cropped .. >
}
outerFunc()
lapply(seq(100), function(i) browser(text=paste("i is #", i),
expr=BROWSE))
Called from: FUN(1:100[[1L]], ...)
Browse[2]>
Called from: FUN(1:100[[2L]], ...)
Browse[2]>
Called from: FUN(1:100[[3L]], ...)
Browse[2]>
Called from: FUN(1:100[[4L]], ...)
Browse[2]>
Called from: FUN(1:100[[5L]], ...)
Browse[2]> BROWSE <<- FALSE ## From here on, browser() won't be triggered
Browse[2]>
[... many snipped lines ...]
Browse[1]> ## so that you're returned to next level
## up without further interruption
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With