1.4 Troubleshooting

It is perfectly normal to encounter errors when writing and running R code. First of all, do not be discouraged! Errors are just R’s way of telling us that it could not understand or execute a command, and the error messages (often displayed in red text in the console) give us clues as to what went wrong. Sometimes the message is straightforward, telling us about a missing package or that we made a typo. Other times, it may be more cryptic. In this case, after carefully reading the error message (which, although technical, is essential as it is our primary source of information), we can ask ourselves the following questions:

  1. Are the necessary packages installed? If the error says something like Error: could not find function "some_function", this is a strong indication that the package containing some_function is not on our system. We can solve this by installing these packages using install.packages() or devtools::install_github().

  2. Are the required packages loaded? Even if we have installed all the required packages, we still need to load them with library(packageName) before R can use them. Forgetting to do this is one of the most common mistakes.

  3. Are the commands, function names and object names spelt correctly? Remember that R is case-sensitive and mydata is different from myData. Typos in function names (intall.packages instead of install.packages) are also common.

  4. Is the data in the correct format? Many R functions expect input data in a particular structure (e.g. a data frame, a vector, a matrix, or an object class such as a quanteda corpus or DFM). Use functions such as class(), str(), summary() or dplyr::glimpse() to inspect a data object and confirm that it matches what the function expects.

  5. Are the packages up-to-date? Sometimes bugs are present in older versions of packages. Updating packages with update.packages() can fix these problems. We can also check if the bug occurred after updating R, as older packages may not be compatible with the latest version of R.

If none of this works, it is time to look for help online. Copying the exact error message and pasting it into a search engine is often the quickest way to find solutions, as many other R users have probably encountered and solved the same problem. If that does not work, there are several other options:

  • Stack Overflow: A widely used question-and-answer site for programmers. Search for any problem or error message. If you need to ask a new question, try to make it as easy as possible for others to help by providing a clear description, the code you ran, the exact error message, and a reproducible example.

  • RStudio Community: This is mainly for problems with RStudio itself, although there are also many threads for package issues.

  • Package documentation and vignettes: The official documentation for packages (accessible via ?functionName or help(packageName)) and longer tutorials called “vignettes” (accessible via vignette(package = "packageName")) often contain solutions or examples that clarify how to use functions correctly.

  • Package websites and GitHub pages: Many packages have dedicated websites or detailed READMEs on GitHub that provide additional information and troubleshooting tips.

When asking for help online, the most helpful thing you can do is to provide a reproducible example. This example, or reprex, is a small, self-contained piece of code that produces the same error or unexpected behaviour you see when run by someone else. This allows others to understand your problem immediately, without needing access to your data or the whole project. The reprex package makes it easy to create these examples in three steps:

  1. Install and load the reprex package
  2. Write the minimal code that causes your problem.
  3. Wrap your code inside the reprex() function and run it. The result (code, output, and error message) will be copied to your clipboard and formatted for pasting into forums like Stack Overflow.
# 1. Install and load the reprex package.

install.packages("reprex") 
library(reprex)

# 2. Write the code that demonstrates your problem. Example: You are trying to use a function from a package that is not loaded.

my_problem_code <- {
  df <- data.frame(a = 1:3, b = c("X", "Y", "Z"))
  dplyr::filter(df, a > 1)   # The issue occurs here as dplyr::filter requires library(dplyr)
}

# 3. Run the problematic code inside the reprex() function.

reprex({
  df <- data.frame(a = 1:3, b = c("X", "Y", "Z"))
  dplyr::filter(df, a > 1)
})

You can also include your sessionInfo() output at the end of your reprex or question. This command shows your version of R, your operating system, and the versions of all packages currently loaded, helping others to identify possible compatibility issues.

Finally, before you ask, it is always a good idea to see if the problem has already been discussed. Searching Stack Overflow or other forums before posting can save time and help keep the community tidy.