13 When your code doesn’t work

Written by Michael Chong and last updated on 7 October 2021.

13.1 Introduction

This lesson contains:

  • suggestions on how to avoid big problems in R
  • possible ways to troubleshoot your R code

Prerequisite skills include:

  • None!

Highlights:

13.2 Avoiding (big) issues

13.2.1 Use common packages where possible

There are a lot of R packages, collectively written by a lot of authors, and this inevitably means some packages may interfere with each other and some packages may not be well-documented. Because of this, as you’re starting out, it’s best to stick with packages that are well-maintained and widely used.

Most of the functions in this toolkit are from the tidyverse suite of packages, which fit these criteria. Try to stick with these functions, because they behave predictably together, and help is easier to find because of their wide use.

13.2.2 Check results as you go

It’s easier to fix a small problem than a big problem. Whenever you’re writing or editing an R file, here are some good practices that I follow:

  1. test your code in your console before making the change in the file
  2. check that the result of your code is what you expect it to be
  3. every once in a while, clear your R environment, run your entire script or document, and check the results again

The third step is really important, because you want to make sure your script or document is self-contained. You want the script to be able to reproduce the results on its own when you come back to it later.

13.3 Dealing with issues on your own

13.3.1 Isolate the issue

Run your code bit by bit until you know which functions are making the problem. In the RStudio editor, you can do this by highlighting a portion of your code, and hitting CTRL/CMD + Enter.

At each point, check that you’re getting what you expect! For example, if your code changing an object over and over:

my_number <- 23
my_number <- my_number * 2
my_number <- my_number - 12

then it can help to print the object after every time you change it:

my_number <- 23
print(my_number)
#> [1] 23

my_number <- my_number * 2
print(my_number)
#> [1] 46

my_number <- my_number - 12
print(my_number)
#> [1] 34

13.3.2 Check that you have the right packages loaded

Did you forget to use library() on the package you need? If you forgot, you probably get the error could not find function. For example, the read_excel() function comes from the readxl package. If you try to use it without loading the package, we get the following error:

read_excel("some_file_name.xlsx")
#> Error in read_excel("some_file_name.xlsx"): could not find function "read_excel"

13.3.3 Check for typos

Seems obvious, but make sure you’re using the variables and functions that you’re intending to use!

13.3.4 Check the built-in help

R and RStudio have great built-in references. To access the help document for a function, use ?function_name. For example, if you want to access the help document for the read_csv() function, type ?read_csv in the RStudio console, which brings up this help page. Equivalently, you can type help(read_csv).

13.4 Escalating the issue

13.4.1 Make a minimal example

Simplify the problem so it’s easier to see what’s going on! There are a couple ways you can do this:

  • try it on a smaller, similar data set
  • make up some fictional in the same format

There’s more on this in [another section]

13.4.2 “General” Resources

These can be good places to start if you’re not familiar with the function or package you’re using.

  • this toolkit!
  • R for Data Science by Hadley Wickham: the definitive online textbook resource for getting started in the tidyverse
  • package vignettes: Some packages have vignettes, which are like user tutorials for the package written by the package authors. You can check for package vignettes using browseVignettes("package_name")

13.4.3 Informal resources

If you know what that what you want to do is a bit unconventional or more complicated than usual, you can try:

  • consult TAs, instructors, or other friendly experienced users
  • search the web! More on this in [another section]
  • ask StackOverflow! More on this in [another section]

13.5 Still stuck?

13.5.1 Ask: “do I have other options?”

Some tasks are just really, really time-consuming or difficult to accomplish in R. Or there are just a lot of tools you would need to know before you can do what you want!

It’s okay to adjust your goal or approach based on what’s feasible! Some ways to do this:

  • break your goal down into smaller parts
  • write “pseudocode” first: write down what you want to do in plain words first, then translate to R code
  • look for packages that al

13.5.2 Remember to take a break!

It can be easy to get stuck thinking about a problem, and become consumed in an endless cycle of Googling and copying and pasting. Take a break and come back.

13.6 Exercises

13.7 Next steps

  • Asking for help? Learn how to make a minimal example in the Making reproducible examples lesson.
  • Having trouble finding what you want on the web? See the lesson Using Google and Stack Overflow and Even more on Stack Overflow for tips on using these tools more effectively!

13.8 Exercises

13.8.1 Question 1

13.8.2 Question 2

13.8.3 Question 3

13.8.4 Question 4

13.8.5 Question 5

13.8.6 Question 6

13.8.7 Question 7

13.8.8 Question 8

13.8.9 Question 9

13.8.10 Question 10