Debugging lapply/sapply calls

后端 未结 7 770

Code written using lapply and friends is usually easier on the eyes and more Rish than loops. I love lapply just as much as the next guy, but how do I debug it when things

7条回答
  •  不知归路
    2020-12-02 09:05

    I've faced the same problem and have tended to make my calls with (l)(m)(s)(t)apply to be functions that I can debug().

    So, instead of blah<-sapply(x,function(x){ x+1 })

    I'd say,

     myfn<-function(x){x+1}
     blah<-sapply(x,function(x){myfn(x)})
    

    and use debug(myfn) with options(error=recover).

    I also like the advice about sticking print() lines here and there to see what is happening.

    Even better is to design a test of myfn(x) that it has to pass and to be sure it passes said test before subjecting it to sapply. I only have patience to to this about half the time.

提交回复
热议问题