1.3 Debugging

It’s easy to make mistakes when you program—even when you are very experienced! Incorrect computer code is said to have a bug, and the art of finding bugs and correcting them is called debugging.

Consider the following code:

scarecrowQuote <- "It is such an uncomfortable feeling to know one is a fool."
paste("The Scarecrow says: ", scarecrowquote)

When we run it we get the following error message in the console:

## Error in paste("The Scarecrow says: ", scarecrowquote) :
##  object 'scarecrowquote' not found

R’s error messages are often quite mysterious—they are intended to be maximally useful to experienced R programmers—but it’s always a good idea to read them anyway. In this case the message clearly tells us the problem: R cannot find the object scarecrowquote on its search path. This prompt us to look more closely at the name scarecrow, and sooner or later we will realize that we have a misspelling: the variable that was actually defined was scarecrowQuote, with a capital Q.

The correct code is:

scarecrowQuote <- "It is such an uncomfortable feeling to know one is a fool."
paste("The Scarecrow says: ", scarecrowQuote)
## [1] "The Scarecrow says:  It is such an uncomfortable feeling to know one is a fool."
Always bear in mind that R is case-sensitive!

Here’s another buggy bit of code:

SermonMountComment <- paste("Oh, it's "blessed are the meek."",
"\nI'm glad they are getting something:\n",
"they have a hell of a time.")
cat(SermonMountComment)

The idea is to produce:

## Oh, it's "blessed are the meek.
## I'm glad they are getting something:
##  they have a hell of a time.

But when we run the code we get the following result instead:

> rm(SermonMountComment)
> SermonMountComment <- paste("Oh, it's "blessed are the meek."",
Error: unexpected symbol in "SermonMountComment <- paste("Oh, it's "blessed"
>                             "\nI'm glad they are getting something: ",
Error: unexpected ',' in "                            "\nI'm glad they are getting something: ","
>                             "they have a hell of a time.")
Error: unexpected ')' in "                            "they have a hell of a time.")"
> cat(SermonMountComment)
Error in cat(SermonMountComment) : object 'SermonMountComment' not found

This can be a bit more difficult to read. The problems appear to start near the beginning of the construction of the string SermonMountComment.

After looking at it a while we focus on the first string argument to the paste() function:

"Oh, it's "blessed are the meek.""

We see that this string has quotes within quotes. Now R uses quotes as delimiters for strings: that is, quote-marks indicate where a string begins and where it ends. Hence from R’s point of view, the first string consists of just: "Oh, it's ". But then there is no comma to separate this string from the next string argument that the paste() functions expects. Instead R sees the b in blessed; that’s an unexpected symbol. Things go downhill from there.

There are a couple of ways to correct the problem. One approach is to use single quotes inside any string that is delimited with double quotes, thus:

SermonMountComment <- paste("Oh, it's 'blessed are the meek.'",
"\nI'm glad they are getting something:\n",
"they have a hell of a time.")
cat(SermonMountComment)
## Oh, it's 'blessed are the meek.'
## I'm glad they are getting something:
##  they have a hell of a time.

On the other hand if you really want those double-quotes inside the string, you can escape their special meaning as string-delimiter by prepending a backslash (\) to them, thus:

SermonMountComment <- paste("Oh, it's \"blessed are the meek.\"",
"\nI'm glad they are getting something:\n",
"they have a hell of a time.")
cat(SermonMountComment)
## Oh, it's "blessed are the meek."
## I'm glad they are getting something:
##  they have a hell of a time.

There are a number of special characters that are formed by “escaping” the usual meaning of some other character. Some common examples are:

• \n: produces a newline instead of n
• \t: produces a tab-space instead of t
• \": produces an actual quote-mark, instead of beginning or ending a string.

Strings are a tricky topic in any computer programming language: in fact we will devote all of Chapter 11 to them.