What to do when your code doesn’t work?

This is the list of actions I make when I have an error in R.

Etienne Bacher

As Jenny Bryan said, one of the first things people do when there is a problem with the code is run it a second time. But in 95% of the cases (in my small programming experience), this does not solve the problem, and I realized I developed a small pattern of reactions when I am in this situation.

Read the error message

This may seem obvious since this is the first thing we see when there is an error. However, some messages (like those in the tidyverse packages) are really helpful and are enough to solve the problem.

As you spend time programming, some error messages become quite familiar over time. You don’t necessarily need to fully understand the message, just remember how you solved the error when you saw this. But this does not help if this is the first time you see the message.

Search on StackOverflow and RStudio Community

If the error message was not helpful, I quickly go to step 2: search online (which is almost always equivalent to “search on StackOverflow and RStudio Community”). Those are the two places where you have the most odds of finding an answer to your problem. If your problem concerns mainstream packages (once again, such as the tidyverse ones), then there will always be at least one person who had a similar problem for which a solution was given.

Search on Github

If no answer was available on these two websites, I usually go a step further and search about it on GitHub, especially if the problem comes from an unknown and/or new package. Indeed, the source code of a lot of R packages is available on GitHub, as well as the brand-new versions (not on CRAN yet). It is also the place where people make feedback on some issues or help develop the package.

It happens that somebody reported a similar issue and that the package maintainer answered. If not, well, it is always useful to see the situations in which people use the package, or to learn new functions of this package.

Post my question online - Part 1

I have an error, I don’t understand the error message and there are no answers online. What can I do now?

You can post your question on one of the websites I mentioned. My order of preference is:

However: posting a question online implies that you need to create a reproducible example. What is a reproducible example?

Suppose that you are working on your data and have a problem. You may want to copy and paste your code online. It’s quick and easy. But try to imagine you’re someone on StackOverflow that sees your code for the first time. This person needs to run it to understand what your code does and what the problem is. But that’s impossible, because you load CSV data that is only available for you, or because you forgot to mention the packages you’re using. Therefore, no one can run your code and the odds of someone solving your problem plummet.

If you post online, your post MUST contain:

Making a reproducible example takes time but is incredibly helpful. Open a new tab in RStudio, try to simplify your situation as much as possible and to make it reproducible by using data available for everyone. For example, some datasets are automatically in R, such as mtcars or iris. Don’t forget to mention the packages you use in library(). Once you think you have finished (you show what packages you use, you keep your example as small as possible and you show the expected output), restart the session (ctrl + shift + F10) and see if it runs and if it reproduces the same error. If it doesn’t, it means your example is not reproducible and that you have to improve it.

I would say that I solve the problem myself by making a reproducible example in 70% of the cases, so taking the time to make one is worth it!

Post my question online - Part 2

However, if you didn’t solve your problem with this (and if your example is reproducible), you can post it on StackOverflow or RStudio Community. Don’t forget to explain your situation and your code, don’t paste it without any details!

Hopefully, you’ll have an answer.

Additional resources

Here are two pages with details about making a reproducible example:


If you see mistakes or want to suggest changes, please create an issue on the source repository.


Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/etiennebacher/personal_website_distill, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".


For attribution, please cite this work as

Bacher (2020, May 22). Etienne Bacher: What to do when your code doesn't work?. Retrieved from https://www.etiennebacher.com/posts/2020-05-22-code-doesnt-work/

BibTeX citation

  author = {Bacher, Etienne},
  title = {Etienne Bacher: What to do when your code doesn't work?},
  url = {https://www.etiennebacher.com/posts/2020-05-22-code-doesnt-work/},
  year = {2020}