Welcome to the Troubleshooting Guide! If you run into errors as we code along during this course, don’t get discouraged! Running into errors is something that all data scientists have in common, so let’s go through some of the most common errors and helpful debugging resources.

What does an error in R look like? It pops up in your console as a different color, usually starts with “Error”, and continues to tell you what specific line of code caused the error and a description.

Package and Library Errors

Library and package errors are one of the most common errors and usually happens early on in your code. A package error tends to happen when you haven’t installed the package yet, but are trying to load it as a library. Let’s try to load in the fortunes package (we haven’t installed this yet!): library(fortunes)

You may have gotten an error like the one in the photo - so how do we fix this? Install the package!

Note: run the install.packages("fortunes") code without the hashtag

#install.packages("fortunes") # run the install code 

Now let’s try to load in the fortunes package one more time:

library(fortunes) # run the library code

Awesome! Another error you might come across is a library error - meaning, you’re trying to use functions from specific packages, but haven’t loaded them in your R session yet. Every time you open up your R session, you have to load in your libraries! Let’s try to use the common ggplot() function from the tidyverse package (remember, we haven’t loaded in the tidyverse package yet): ggplot(data=iris)+ geom_point(mapping=aes(x=Petal.Length,y=Petal.Width))

It says it could not find the function “ggplot”… we can fix this by running the library code to load in the tidyverse package.

library(tidyverse)
## ── Attaching core tidyverse packages ─────────────────── tidyverse 1.3.2.9000 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.3
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.2.1
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()  masks stats::filter()
## ✖ lubridate::hms() masks vembedr::hms()
## ✖ dplyr::lag()     masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Try the ggplot code again:

ggplot(data=iris)+
  geom_point(mapping=aes(x=Petal.Length,y=Petal.Width))

Great job!

File path Errors

File path names are super important to know how to get, especially when you’re trying to import files into your R session. Try to import the dataset by using the read_csv() function and the name of your dataset as your input: filepath_data <- read_csv("SDR-2023-Data.csv")

You probably got an error saying that the data doesn’t exist, but we know that it does. It’s because we didn’t write in the proper file path name.

How do you get the file path name?

  • For Mac users, you can read this article under Show the path to a file or folder.
  • For Windows users, you can read this article

Let’s try to read in our data with the proper file path name:

# filepath_data <- read_csv("/Users/victoriadelacruz/Desktop/R/Projects/SDR-2023-Landscape/data/SDR-2023-Data.csv")

Note: your computer user name will most likely show up in the file path name along with the different folders your R project and data are in (no one will have the same exact file path name)

Success!

Syntax Errors

Syntax errors occur when you have missing or unmatched parentheses, curly braces, square brackets, or quotes. Try to run this code with a missing parenthesis: library(tidyverse

You probably didn’t get an error message, rather, a couple of plus signs. This means that your code is incomplete. Add the missing parenthesis and you’re good to go!

Typos

Although typos may not seem like a big deal, they definitely are in code! Let’s attempt to print the filepath_data variable we made earlier:print(filepath_dat)

As you can see, the error message we got says that ‘filepath_dat’ was not found - this is because we have a typo. These can be tricky to find as it doesn’t explicitly tell us that it was a spelling error. Let’s correct our typo:

# print(filepath_data)

Looks great!

Solutions and Resources

There are going to be different types of errors you might encounter that was not covered in this troubleshooting guide. Here are some tips to help you resolve them:

  1. Google Search

Google will be your best friend when it comes to solving errors! You can copy and paste your error into a Google search, and it will show you a bunch of resources and solutions others have done (chances are, someone else has run into the same or similar error).

  1. ChatGPT

You can do the same with ChatGPT - copy and paste your error and it will lead you to some possible solutions. You may have to play around with what ChatGPT provides (for example, making sure your variable names are correct) to ensure you’re running the code you intended to.

  1. Help Page

R has useful help pages for almost all packages and functions. For easy access to the help page, you can type ?function_name() or ?package_name. Let’s try this out with the ggplot() function:

?ggplot() # type in the function or package name after the question mark

This code should have opened up the help page on the side. This contains what arguments the function takes in as well as some examples you could run to better understand how it works.

  1. Resources

After doing a Google search, you’ll get a bunch of resources that you can look to. Here are some recommendations: - W3 Schools - Geeks for Geeks - Stack Overflow - R Bloggers