Appendix G. Troubleshooting: Why Doesn’t My Code Work?

Beginners at almost anything will make mistakes. Fortunately, beginners with R receive error messages when they make mistakes, indicating what needs to be changed. Unfortunately, those messages are often cryptic and seem to be written in a language that only vaguely resembles English. A little experience with R will smooth the difficulties in a short time. The following pages include some examples of easily made errors and messages that accompany them. You should know that I have made each of the following errors myself, some of them many times!

Misspelling

One of the most common mistakes is simple misspelling. R, however, apparently does not have the word “misspelling” in its vocabulary and therefore labels this problem in sundry other ways:

> attach(trees)
> plt(Girth, Height)
Error: could not find function "plt"

In this example, R suggested that we wanted to use a function that does not exist, or is hiding, but it was a simple spelling error! Retype the command with the correct spelling:

> plot(Girth, Height)

Actually, you do not need to retype the whole command. In the preceding example it is not a big deal, but sometimes you will have a long line that it would be quite tedious to repeat. In that case, there are two shortcuts:

  • Copy and paste the incorrect line to the most recent prompt (>) and fix the error before pressing Return (or Enter).

  • Press the up-arrow key; the previous line will be copied, and you can edit ...

Get Graphing Data with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.