[R] debug on lapply

jim holtman jholtman at gmail.com
Tue Dec 11 19:47:24 CET 2012


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.




More information about the R-help mailing list