Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested browser calls -- exiting only a single context

Tags:

r

debugging

UPDATE:

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.

________________________________________________________________

Original Question:

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.

The Question is:

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)))

this is the walk through

> 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
> 
like image 477
Ricardo Saporta Avatar asked Oct 22 '13 15:10

Ricardo Saporta


1 Answers

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
like image 164
Josh O'Brien Avatar answered Sep 23 '22 19:09

Josh O'Brien