Chapter8 Debugging in R
Up to this point, you have learned how to write R code to manipulate data, perform calculations, use loops, and apply functions efficiently. Inevitably, as your code becomes longer and more complex, things will go wrong. This is normal.
Debugging is not a sign of failure or lack of ability — it is a core skill of every programmer, data scientist, and actuary. In practice, a significant proportion of your time will be spent finding, understanding, and fixing errors, rather than writing new code from scratch.
This chapter focuses on:
- understanding different types of errors in R
- developing a systematic approach to debugging
- learning common tools and habits that make debugging easier
The aim is not to eliminate errors (that is impossible), but to help you locate and fix them efficiently and confidently.
8.1 Types of errors in R
When something goes wrong, R usually tells you something — but not always in a way that is immediately clear. Broadly, errors fall into three categories.
- Syntax errors
Syntax errors occur when R cannot understand what you have typed. These are usually caused by:
- missing brackets
(),{},[] - missing commas
- unmatched quotation marks
- incorrect function structure
R will complain because mean() expects a single vector, not multiple arguments.
- Run-time errors
Run-time errors occur when the code is syntactically correct, but R cannot execute it:
This returns NA, which may silently cause problems later in your code.
- Logical errors (the most dangerous kind)
Logical errors occur when:
- the code runs
- no error messages appear
- but the result is wrong
The code is valid, but the calculation may not reflect what you were intending.
8.2 Systematic debugging workflow
When your code does not behave as expected, avoid randomly changing things and hoping it works. Instead, follow a structured approach.
- Read the error message carefully - Error messages often contain:
- the type of error
- the function involved
- sometimes the line number
Do not ignore warnings — they frequently indicate deeper problems.
- Check object existence and names
Many bugs are caused by misspelled or overwritten object names.
- Check object types and dimensions
Many functions behave differently depending on object type.
- Simplify the problem
Run your code line by line and inspect intermediate objects. Debugging becomes much easier when problems are isolated.
- Use print statements deliberately
Printing intermediate values inside loops or functions is one of the most effective debugging techniques.
- Check edge cases and missing values
Always consider:
NAvalues- empty vectors
- divisions by zero
- first and last loop iterations
8.3 Useful debugging tools
traceback()
Shows the sequence of function calls that led to an error.
browser()
Pauses execution so you can inspect objects interactively.
8.4 Good debugging habits
- Assume the issue is in your understanding, not R
- Test code on small examples
- Validate results using intuition
- Never trust output you do not understand
Most serious errors come from silent logical mistakes, not crashes.