Debugging Tools
Diagnosing the Problem
Indications that something’s not right
- message: A generic notification/diagnostic message produced by the message function; execution of the function continues
- warning: An indication that something is wrong but not necessarily fatal; execution of the function continues; generated by the warning function
- error: An indication that a fatal problem has occurred; execution stops; produced by the stop function
- condition: A generic concept for indicating that something unexpected can occur; programmers can create their own conditons
How do you know that something is wrong with your function?
- what was your input? How did you call the function?
- What were you expecting? Output, messages, other results?
- What did you get?
- How does what you get differ from what you were expecting?
- Were your expectations correct in the first place?
- Can you reproduce the problem(exactly)?
Basic Tools
The primary tools for debugging functions in R are
- traceback: prints out the function call stack after an error occurs does nothing if there’s no error
- debug: flags a function for “debug” mode which allows you to step through execution of a function one line at a time
- browser: suspends the execution of a function wherever it is called and puts the function in debug mode
- trace: allows you to insert debugging code into a function a specific places
- recover: allows you to modify the error behavior so that you can browse the function call stack
These are interactive tools specifically designed to allow you to pick through a function. There’s also the more blunt technique of inserting print/cat statements in the function.