General suggestions for debugging in R

后端 未结 13 2331
梦毁少年i
梦毁少年i 2020-11-22 12:26

I get an error when using an R function that I wrote:

Warning messages:
1: glm.fit: algorithm did not converge 
2: glm.fit: algorithm did not converge 
         


        
13条回答
  •  失恋的感觉
    2020-11-22 13:16

    So browser(), traceback() and debug() walk into a bar, but trace() waits outside and keeps the motor running.

    By inserting browser somewhere in your function, the execution will halt and wait for your input. You can move forward using n (or Enter), run the entire chunk (iteration) with c, finish the current loop/function with f, or quit with Q; see ?browser.

    With debug, you get the same effect as with browser, but this stops the execution of a function at its beginning. Same shortcuts apply. This function will be in a "debug" mode until you turn it off using undebug (that is, after debug(foo), running the function foo will enter "debug" mode every time until you run undebug(foo)).

    A more transient alternative is debugonce, which will remove the "debug" mode from the function after the next time it's evaluated.

    traceback will give you the flow of execution of functions all the way up to where something went wrong (an actual error).

    You can insert code bits (i.e. custom functions) in functions using trace, for example browser. This is useful for functions from packages and you're too lazy to get the nicely folded source code.

提交回复
热议问题