Debugging lapply/sapply calls

后端 未结 7 737

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:02

    If you wrap your inner function with a try() statement, you get more information:

    > sapply(x, function(x) try(1/x))
    Error in 1/x : non-numeric argument to binary operator
    [1] "-0.5"                                                    
    [2] "Error in 1/x : non-numeric argument to binary operator\n"
    [3] "Inf"                                                     
    [4] "1"                                                       
    [5] "0.5"
    

    In this case, you can see which index fails.

提交回复
热议问题