R Error Guide
12th December 2022 By MARS Research Hub

This is an article. It advises you the best practices for debugging your R programming code, when errors are happening or occur

Common Mistakes 

Wow, what a big mistake, something must have gone wrong or you didn't understand something.

Here are some of the most common errors that even seasoned R users make:

  1. Capitalization

You typed a capital letter when you should have typed a lowercase letter (or vice versa). Remember that capitalization matters. Library() and library() are not the same. Library() will result in an error code 

  1. Misspelling

It is worth noting that R won't highlight your mistakes as they're typing, unlike Microsoft Office. It will, however, accept both American and British terms like color/colour. Or organization/ organisation."

  1. Closing Punctuation

You forgot a closing parentheses, bracket, or quotation. All too often have I forgotten to add an additional parenthesis at the end of a line. You'll know that you've done this if you see a red X on the left side. The red X will appear as you are typing so wait until you’re finished to assess these warnings. Please copy-paste your code from a more reliable application before pasting into R

  1. Continuing Punctuation

You missed adding a comma or pipe at the end of your sentence before starting the next line. The indentation is there to make it easier to read and spot errors, but sometimes you can forget them:

  1. Conflicting code

Maybe you mistakenly changed a definition for an object. Try clearing the environment and running some lines of code at a time to see where the problem is.

  1. Libraries Are Not Loaded

It is essential to make sure that all the necessary libraries are loaded when you use R. Once you exit out of RStudio, the libraries will be unloaded.. R will sometimes install updates or unload any unexecuted portions of code during a session. If an error reads that a function cannot be found, the solution is to reload your libraries.

  1. The Unsaved Object

As a refresher, the  <- symbol represents “defines”. The syntax is typically structured as such:

name.of.object <- "the definition of the object goes here"

It's best to "test" the definition before defining it as an object. That is, you should know what the definition is before you define it because then the definition will be more accurate. What if you accidentally defined an object poorly? When you use an undefined object, you almost always produce additional errors which make it impossible to display the object’s definition.

One Line at a Time

A good rule of thumb is to run a few lines at a time to identify the problematic code.

Whenever Googling Doesn't Work

Sometimes you just can’t find an answer.

Sometimes the RStudio software can have a few issues that are unrelated to your code. If your code keeps breaking, try simply restarting the software or your computer - it's worth a shot!.

Make sure you save the information you need to save before closing RStudio or restarting your computer.

Upon reopening Rstudio, make sure the Global Environment is clean before executing code.

The other option is to not worry about it. The little elves will come and fix everything for you while you're gone. If you stay, they'll never show up!

Error messages can be really anger- inducing and when you're in a cloud of frustration, it can be hard to think clearly. Try stepping away for a bit and thinking about it from a different emotional state.

Finally, ask MARS team for help. MARS TEAM ARE experienced R users who won’t take a long time to find out what went wrong and it's often difficult for beginners to figure out the source of the problem. We all make mistakes. But, the experienced experts among us know what the issue might be. If you're a beginner though and aren't sure what to do, then don't worry- just ask someone who has more experience than you!. Solving problems is one of the best ways to learn, but it can also be time consuming. If you need help, don’t hesitate to ask for it!