Troubleshoot in R


When you start coding in R you probably get a lot of errors, and trying to decipher an error can be a time-consuming task. You might need to Google or maybe you asked for help to your friends/mentor and they find out you’ve forgot a closing bracket!.

You just learnt about the importance of it just few hours ago, “You’re supposed to know this stuff”. You might be frustrated and blame yourself when you make very basic mistakes, but this is something every one goes through.

This is the beginner curse: to become better, you need to practice writing code. This article will help you so you can troubleshoot your own error in better way.

Learning objectives

  • Understand basic error that beginner usually encounter
  • Learn to solve an error in your code

Know your error!

Pay attention to your code line for a mark’s!

Example of error code in line:

Before running, it’s very convenient to know exactly on which line you made a mistake. Read the error message in the console, an error will appear after you run a wrong code.

Example of error code in console:

The most common error in RStudio is Syntax errors. You’be forgotten a comma, opened a bracket, but haven’t closed it, misspelled character by mistake or something else R doesn’t understand. Those are usually picked up by R and you will get error messages reminding you to proof-read your code and fix them.

Here some error that newbie in R probably encounters:

  • ‘could not find function’. This error happen when an R package is not loaded properly or due to missing object like misspelling of the functions or data set name. Example below show eror massage that R can’t find function because we misspelled a function called ‘boxplot’ with ‘boxsplot’.

  • ‘Error in if’. It generally means the logical statement in “if (xxx) { …” is not yielding a logical value. Most of these have missing value where TRUE/FALSE is needed, meaning that the variable in xxx has NA in it.

  • ‘object not found’. This error occurs when the particular object used in the code is empty.

  • ‘cannot open the connection’. There can be a reasons we are getting this error:
    • A file/connection can’t be opened because R can’t find it (mostly due to an error in the path).
    • Failure in .onLoad() because a package can’t find a system dependency.
  • “subscript out of bounds”. This error is likely to occur when one is using nested loops incorrectly in the code.

  • ‘non-numeric argument to a binary operator’. This is a simple error to decipher. This happen when we mix different vector value in calculation, for the example : numeric x characters.

  • “replacement has”. This error occurs when one tries to assign a vector of values to a subset of an existing object and the lengths do not match up.

    R is case sensitive, so be mind of capital letter in your code too. Try to capitalize the use of complition engine by R-bloggers and for more in-depth of the topic read the article by R-bloggers that listed more beginner error. If you can’t pinpoint the correct way to code what you need, there are many places to find help.

Troubleshoot the Error!

R is an open source language, R is backed by a large community of users and resources for help. Our Getting Help with R Knowledge Base article points to some of the more well-known resources to help solve your R related problem.

  • Read the Documentation

    R’s built-in help utility provides detailed descriptions of R topics, functions, and their arguments. To show these help files, use the Help pane within RStudio. Alternatively, to access this help from the console for a particular R topic or function, enter the following commands in console: ?help or ?(package) or ?(function).

    If your error has to do with a specific Library, it is quite possible that there is some concept you may not know about that is the hidden cause of your error. Look at the examples and understand how you might differ from that.

  • Google it!

    Usually for an inexperienced coder, the trouble with tackling projects with an infinite number of methods is that you don’t necessarily know what the language can or can’t do. But plugging all error message into Google isn’t going to help! Why? Because you’ve left a bunch of assumptions in there that only you are going to use.

    Google searches should be simple, but broad enough to bring you the anticipated results. Sadly, sometimes the problem was that we didn’t even know how to get to the level of correct specificity. I Suggested that you to “search to find better search terms.”

    So if you have an error code, Google that. If that doesn’t work, Google the error message. If that doesn’t work, Google the library you’re using and the least is syntax would be my general guide, in descending order of usefulness with other keywords.

  • Reproduce the Error

    Start a whole new project and make it very small so that you can isolate your Error. Copy over the bare minimum code from your existing project that you will reproduce the Error, or just try to code it up from scratch without all the extra fluff your main project has.

    If you cannot reproduce the error, you will have found a clue as to what is going on with error in your code. If you CAN reproduce the Error, great! because it would help you with the next step.

  • Asking for Help

    Post your error in forum. Github, Stackoverflow, Reddit, Twitter, Slack/Discord communities or you may also ask for help from R and RStudio users on Ask the question in sensible way, Stackoverflow make a good article about how to improve your chance to get an answer and be sure to include a reproducible example of your issue. For rule of thumb you post your ‘sessionInfo()’,

and when you having trouble applying an operation to some R object, but want to avoid the potential hassle of transferring files and code to another person’s computer, make a reproducible example and/or use ‘dput()’ to efficiently convey the problematic object. For the example i tried to applied it in my cd object. ‘dput()’ will write an ASCII text representation of an R object to a file or connection, or to recreate the object.

You should post your minimally reproducible sample. Try reprex package torender bits of code that you have problem with. Learn more about reprex as a way to show your problematic code and it will help go a long way for people to figure out what is going on.

Be better at writing a code!

This article by Coding Club can be your guideline of writing better line of code that “neat-er” and make it easier to troubleshoot.

Scroll to Top