3  Getting help and dealing with errors

When learning a new language, mistakes are inevitable, and programming can be particularly frustrating due to the myriad of errors one can encounter. Unlike practicing a foreign language with speakers who may be forgiving of mistakes, R is exceptionally unforgiving. If your query is not structured correctly, it won’t be understood at all. Therefore, it’s crucial to find ways to pinpoint and understand the root causes of these errors. Overcoming errors is one of the most effective methods for learning R. Here are a few places where you can find the help you need:

3.1 R documentation

Every function or package in R comes with documentation provided by its creators. You can access this documentation directly from RStudio by placing a ? before the function or package name in the console and executing the command. This will open the documentation in the help pane, where you’ll find a description of the function, its various arguments, and some usage examples.

?readr::read_csv # Access the documentation of the read_csv function

3.2 Cheatsheets

Every package in the tidyverse (and some others) has a cheatsheet that provides information about its various functions. You can find these cheatsheets at this link For example, check out the readr cheatsheet.

3.3 Online ressources

First, remember that you’re not alone in encountering errors in R; many others have faced similar issues before you. Often, they have sought help online. If R gives you an error you don’t understand, it’s likely that someone else has encountered the same issue and discussed it online. Start by checking if others have asked similar questions. Websites dedicated to R programming frequently have solutions from experienced users. Additionally, Google can be a valuable resource. Whether you’re trying to accomplish a task (e.g., “how to import a .dta file in R”) or resolve an error, searching the error message or your query can provide valuable insights.

Often, you’ll find yourself on a site called Stackoverflow, a community hub for users of various programming languages. You can often copy and paste the code you find there, but it’s important to tweak and adapt it to fit your specific needs. Remember, in the world of programming, it’s common for everyone to borrow and adapt code from others. Another helpful resource is the Rstudio Commmunity. Additionally, the #rstats hashtag on Twitter can provide insights and discussions from the R programming community.

3.4 AI is your friend

Lastly, we’ve entered the era of generative AI. When coding, Large Language Models, particularly ChatGPT, can swiftly emerge as invaluable allies. By supplying ChatGPT with your errors or seeking guidance for specific coding tasks, you can obtain outstanding results. I strongly suggest incorporating it into your coding journey. However, exercise caution: ChatGPT might sometimes suggest non-existent functions or present inaccurate information. It isn’t a magic bullet, but you can quickly assess the accuracy of its suggestions by testing the code in R. If the code doesn’t work correctly, the information may be incorrect. For instance, check out this example example where I asked ChatGPT to explain how to import a Stata file in R.

3.5 Most common errors

Finally, some errors are really common and you will probably face them often. I provide you here a (non exhaustive) list of those to help you troubleshooting1.

3.5.0.1 Syntax errors

Many errors beginners encounter in R stem from syntax issues: a slight coding mistake can lead RStudio to misunderstand your intentions. Common errors include typos in function names or forgetting symbols like ), ,, or ". For example, if you missed a closing " when trying to subset the Le Pen string from the leaders vector: leaders[leaders == "Le Pen], you’d likely see a + in the console. This indicates that R is awaiting further input to process your command.

3.5.0.2 The “not found” errors

  • Error: function 'x' not found : mispelling or package not loaded
Library(tidyverse)
Error in Library(tidyverse): could not find function "Library"
means(c(15,16,19)) 
Error in means(c(15, 16, 19)): could not find function "means"
read_html("https://labour.org.uk/category/latest/press-release/") # Read html code from a webpage
Error in read_html("https://labour.org.uk/category/latest/press-release/"): could not find function "read_html"

Mistakes related to capitalization or misspelling are common. For instance, attempting to compute the mean of a number vector but mistakenly adding an “s” to the mean() function will lead to an error. In another scenario, you might aim to read a webpage’s HTML code for web scraping purposes. While the function might be correctly spelled, the error arises if the required rvest package isn’t loaded beforehand. When encountering such errors, ensure you’ve spelled functions correctly and loaded the necessary package (e.g., using library(rvest)).

  • Error: object 'x' not found : typo, forgot to run the line or saving object
leaders <- c("Chassaigne", "Vallaud", "Chatelain", "Panot")
leader[1]
Error in eval(expr, envir, enclos): object 'leader' not found

You might alo want to look only at leaders from right-wing parties in the object right_wing_leaders Here, the error happens because we did not save any object with this value yet.

right_wing_leaders
Error in eval(expr, envir, enclos): object 'right_wing_leaders' not found
right_wing_leaders <- c("Waucquiez")
right_wing_leaders
[1] "Waucquiez"

Error in install.packages : object 'x' not found

Error in eval(expr, envir, enclos): object 'rvest' not found

Most of the time, you just forget the "" and you should write install.packages("rvest"). It might also be a typo in the package name (eg. you would have an error with install.packag("Rvest").

  • Error: 'x' does not exist in current working directory
readr::read_csv("thisdata.csv")
Error: 'thisdata.csv' does not exist in current working directory ('/Users/malo/Documents/teaching/2024_intro_r/session01').

This error typically arises when you’ve given an incorrect path, and R can’t find your file. Use getwd() to check your current working directory and then adjust the file path as needed.

3.5.0.3 Inconsistent data types

We have seen already that R comes with different data types such as logical or character. Many functions takes as argument a vector of a specific type and will not work on other. Below an obvious example : if we try to compute the mean of a character vector, this will not work.

leaders
[1] "Chassaigne" "Vallaud"    "Chatelain"  "Panot"     
class(leaders)
[1] "character"
mean(leaders)
Warning in mean.default(leaders): argument is not numeric or logical: returning
NA
[1] NA

  1. I rely on many blogposts, here, here, here, here, here and here↩︎