1.10 Diagnosing Problems

The learning objectives of this section are to:

  • Describe how to diagnose programming problems and to look up answers from the web or forums

Inevitably, no matter what your level of expertise, you will get to a point in your R programming where you’re stuck. It happens to us every single day.

The first question is always “How do you know you have a problem?” Two things must be satisfied in this situation:

  1. You had a certain expectation for what was supposed to happen
  2. Something other than that expectation actually happened

While it might seem overly didactic to separate out these two things, one common mistake is to only focus on the second part, i.e. what actually happened. Typically, we see an error message or a warning or some other bad sign and we intuitively know that there is a problem. While it’s important to recognize these warning signs, it’s equally important to be able to say specifically what your expectation was. What output were you expecting to see? What did you think the answer was going to be?

The more specific you can be with your expectation, the more likely you’ll be able to figure out what went wrong. In particular, in many cases it might be that your expectations were incorrect. For example, you might think it’s a bug that the log() function returns NaN when called on a negative number. If you were expecting there to be an error in this situation, then your expectation is incorrect because the log() function was specifically designed to return the NaN value (indicating an undefined operation) and give a warning when called with negative numbers.

There are two basic approaches to diagnosing and solving problems.

  1. Googling
  2. Asking a human

Before asking a human, it’s usually best to see if you can Google your way out. This can be a real timesaver for all involved. We discuss both approaches below.

1.10.1 How to Google Your Way Out of a Jam

Like with any other programming language, it’s essential that you know how to Google your way out of a jam. A related resource in this situation is the Stack Overflow web site, which is a popular Q&A web site for programming related questions. However, often results from Google will simply point you to Stack Overflow, so Google can serve as useful wrapper around a variety of web site like this.

While we don’t exactly have an algorithm for getting unstuck from a jam, here are few tips.

  • If you get an error message, copy and paste the entire error message into Google. Why? Because, almost surely, someone else has gotten this very same error and has asked a question about it on some forum that Google has indexed. Chances are, that person copy-and-pasted the error message into that forum posting and, presto! You have your answer. Or something close to it.

  • For working with certain high-level functions, you can simply Google the name of the function, perhaps with the phrase “R function” following it in case it is a somewhat generic function name. This will usually bring up the help page for the function first, but it will also commonly bring up various tutorials that people have written that use this function. Often, seeing how other people use a certain function can be very helpful in understanding how a function works.

  • If you’re trying to learn a new R package, Google “[package name] vignette” and “[package name] tutorial.” Often, someone will have written course slides, a blog post, or a document that walks you through how to use the package.

  • If you are struggling with how to write the code for a plot, try using Google Images. Google “r [name or description of plot]” (e.g., “r pareto plot”) and then choose the “Images” tab in the results. Scroll through to find something that looks like the plot you want to create, and then check the image’s website. It will often include the R code used to create the image.

1.10.2 Asking for Help

In the event that Googling around does not find you an answer, you may need to wade into a forum like Stack Overflow, Reddit, or perhaps the R-help mailing list to get help with a problem. When asking questions on a forum, there are some general rules that are always worth following.

  • Read the posting guide for the forum, if there is one. This may cover the rules of posting to the forum and will save you a bit of grief later on.

  • If the forum has a FAQ, read it. The answer to your question may already be there.

  • State the problem you’re trying to solve, along with the approach that you took that lead to your problem. In particular, state what you were expecting to see from your code. Sometimes the source of your problem lies higher up the chain than you might think. In particular, your expectations may be incorrect.

  • Show that you’ve done your homework and have tried to diagnose the problem yourself, read the help page, Googled for answers, etc.

  • Provide a reproducible example of your problem. This cannot be stressed enough. In order for others to help you, it’s critical that they can reproduce the problem on their own machines. Otherwise, they will have to diagnose your problem from afar, and much like with human beings, this is often very difficult to do. If your problem involves massive amounts of computation, try to come up with a simple example that reproduces the same problem. Other people will not download your 100 GB dataset just so they can reproduce your error message.