Dear R experts, recently I tried to debug a R function with an internal lapply call. When debugging I seem not to be able to use the "n" command to debug the inner function called by lapply. How could I achieve this? *For example:* test <- function( ) { lapply( 1:3, function( x ) x + 1 ) } debug( test ) *Start debug:*> test()debugging in: test() debug bei #1:{ lapply(1:10, function(x) x + 1) } Browse[2]> n debug bei #2:lapply(1:10, function(x) x + 1) *The next "n" does not allow me to inspect the inner function,* *but gives me the result directly:* Browse[2]> n exiting from: test() [[1]] [1] 2 [[2]] [1] 3 [[3]] [1] 4 Can anyone help me, please? Kind regards! [[alternative HTML version deleted]]
Typically when I am debugging an 'lapply', I put a browser call inside the lapply function that is being called and then make sure I 'source' in the code instead of copy/paste -- the copy/paste will use any trailing statements after the lapply call as commands to the "browser" function. Here is the modified code I used: lapply( 1:3, function( x ){ browser() x + 1 } ) Here is the output from the console after "sourcing" in the statements above:> source('clipboard')Called from: FUN(1:3[[1L]], ...) Browse[1]> ls() [1] "x" Browse[1]> x [1] 1 Browse[1]> n debug at clipboard#3: x + 1 Browse[2]> x [1] 1 Browse[2]> n Called from: FUN(1:3[[2L]], ...) Browse[1]> x [1] 2 Browse[1]> n debug at clipboard#3: x + 1 Browse[2]> Called from: FUN(1:3[[3L]], ...) Browse[1]> n debug at clipboard#3: x + 1 Browse[2]> x [1] 3 Browse[2]> n>On Tue, Dec 11, 2012 at 1:17 PM, Asis Hallab <asis.hallab at gmail.com> wrote:> Dear R experts, > > recently I tried to debug a R function with an internal lapply call. > When debugging I seem not to be able to use the "n" command to debug the > inner function called by lapply. > How could I achieve this? > > *For example:* > test <- function( ) { > lapply( 1:3, function( x ) x + 1 ) > } > debug( test ) > > *Start debug:* >> test() > debugging in: test() > debug bei #1:{ > lapply(1:10, function(x) x + 1) > } > Browse[2]> n > debug bei #2:lapply(1:10, function(x) x + 1) > > *The next "n" does not allow me to inspect the inner function,* > *but gives me the result directly:* > Browse[2]> n > exiting from: test() > [[1]] > [1] 2 > > [[2]] > [1] 3 > > [[3]] > [1] 4 > > Can anyone help me, please? > Kind regards! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On 11/12/2012 1:17 PM, Asis Hallab wrote:> Dear R experts, > > recently I tried to debug a R function with an internal lapply call. > When debugging I seem not to be able to use the "n" command to debug the > inner function called by lapply. > How could I achieve this?Jim gave you one solution. Another is to debug lapply, and when stepping through it, debug(FUN) after it is created on the first line. Jim's method is more convenient if you are particularly interested in a complicated function, this method is better if you are already in the middle of debugging and just want to look at a possibly anonymous function being passed to lapply. Duncan Murdoch> > *For example:* > test <- function( ) { > lapply( 1:3, function( x ) x + 1 ) > } > debug( test ) > > *Start debug:* > > test() > debugging in: test() > debug bei #1:{ > lapply(1:10, function(x) x + 1) > } > Browse[2]> n > debug bei #2:lapply(1:10, function(x) x + 1) > > *The next "n" does not allow me to inspect the inner function,* > *but gives me the result directly:* > Browse[2]> n > exiting from: test() > [[1]] > [1] 2 > > [[2]] > [1] 3 > > [[3]] > [1] 4 > > Can anyone help me, please? > Kind regards! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.