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:
- test your code in your console before making the change in the file
- check that the result of your code is what you expect it to be
- 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:
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