::read_csv # Access the documentation of the read_csv function ?readr
6 Getting help and dealing with errors
When learning a new language, mistakes are inevitable. 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 you don’t structure your query correctly, it won’t understand at all. Thus, it’s crucial to find ways to pinpoint and comprehend the root causes of these errors. Overcoming errors is one of the most effective methods to learn R. Here are a few places where you can find the help you need
6.1 R documentation
Every function or package in R has documentation written by its creators. You can access this documentation directly from RStudio by placing a ?
in front of the function or package name in the console and executing that line of code. This action will open the documentation in the help pane, displaying a description of the function, the various arguments it can take, and a few usage examples.
6.2 Cheatsheets
Every package in the tidyverse (and some others) has a cheatsheet filled with information about the different functions it contains. You can find these cheatsheets at this link For example, check out the ‘readr’ cheatsheet.
6.3 Online ressources
First, understand that you’re not alone in encountering errors in R; many before you have faced similar issues. Often, they’ve sought help online. So, if R gives you an error you don’t comprehend, it’s probable that someone else has faced it and discussed it online. Start by checking if others have posed similar questions. Sites dedicated to R programming often have solutions from experienced users. Additionally, Google can be a great ally. Whether you’re trying to accomplish a task (e.g., “how to import a .dta
file in R”) or resolve an error, simply searching the error message or your query can yield 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.
6.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’ll quickly discern the accuracy of the information because, if it’s off the mark, the code won’t execute correctly in R. Just have an look on this example where I asked it to explain how to import a stata file in R.
You can also interact with ChatGPT directly in Rstudio. To to so, you will need to have an OpenAI account and install the askgpt
package developed by Johannes Gruber. It is not free, but still cheap.
6.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.
6.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 Abascal
string from the candidates
vector: candidates[candidates == "Abascal]
, you’d likely see a + in the console. This indicates that R is awaiting further input to process your command.
6.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
<- c("Diaz", "Sanchez", "Abascal", "Feijoo")
candidates 1] candidate[
Error in eval(expr, envir, enclos): object 'candidate' not found
You might alo want to look only at candidates from mainstream parties in the object mainstream_candidates
Here, the error happens because we did not save any object with this value yet.
mainstream_candidates
Error in eval(expr, envir, enclos): object 'mainstream_candidates' not found
<- candidates[!candidates %in% c("Abascal", "Diaz")]
mainstream_candidates mainstream_candidates
[1] "Sanchez" "Feijoo"
Error in install.packages : object 'x' not found
install.packages(rvest)
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
::read_csv("thisdata.csv") readr
Error: 'thisdata.csv' does not exist in current working directory ('/Users/malo/Library/CloudStorage/GoogleDrive-malo.jan@sciencespo.fr/Mon Drive/Teaching/2023_intro_r/session1').
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.
6.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.
candidates
[1] "Diaz" "Sanchez" "Abascal" "Feijoo"
class(candidates)
[1] "character"
mean(candidates)
Warning in mean.default(candidates): argument is not numeric or logical:
returning NA
[1] NA