Appendix E — Checklist: handling code errors
When you run some code in R and it produces an error, follow the steps learned in this checklist to identify and fix the problem.
- Read the error message and fix any obvious problems.
- Make sure your code follows the style guide introduced in Chapter 5, which will make it easier to see where there might be problems, especially those caused by typos. In particular, make sure that each function is on a separate line so you can comment them out individually. Remember you can use the styler package (see Section 5.8 for details) to automatically style your code.
- Comment out all the lines of your code. Uncomment each line in turn (starting with the first line), re-running the code each time until you see the error you’re trying to understand. Now you know which line is likely to be causing the problem.
- If the error message is one of the common errors listed in Appendix C, follow the steps outlined in Appendix C to fix it.
- If that does not fix the problem, read the manual page for the function that you think is causing the problem. Check that the values you have passed to each argument are what the function needs in order to work.
- If that does not fix the problem, create a reproducible example (also called a reprex or a minimal working example) as explained in Section 8.6. Often, the act of creating a reproducible example will help you fix the problem, but if not then you can use it to get help from others.
You will be able to fix almost all the errors in your code by following the early steps in this list. Only quite rarely will you need to post a reprex online to get help.
You can find more details about how to deal with bugs in your code in Chapter 8.