3MW (Why you should use RStudio projects)

Guten Tag!

Many greetings from Munich, Germany. I recently came to realize that way too many people who use R & RStudio do not use RStudio projects. 🤯 That is super unfortunate because it’s a low-effort, high-gain productivity boost to use this RStudio feature. So in this week’s newsletter let me tell you about the advantages of RStudio projects and show you how to set them up (which is super easy.)

Advantage 1: No need to think about paths

If there’s one thing I find highly annoying, then it’s path switching and setting working directories. And the good news is that you don’t really have to use setwd() and then include some long-winded path. All you have to do is to set up an RStudio project and wherever that project lives, that’s the working directory.

Advantage 2: Easy navigation between projects

RStudio projects make it super easy to navigate between other projects. You could easily have two projects open at the same time within two different RStudio sessions. This way, if you repeat something from another project, you can easily switch to that project, see all the files, get that code from over there, and then put this into your new project.

And in case you need to run the old code to remember how to use it, you can do that directly within your RStudio session. Due to the separate project, you don’t need to switch working directories in order for code to just work like it did last time when you ran it.

Advantage 3: RStudio projects remember where you left off

When you open RStudio projects, they show you where you left off. The same files are opened and you can just work as if you’ve just closed down your computer and fired it up again. This also means that you don’t have to close all the files from one project and open all the files from another project when you have to switch between two projects.

Advantage 4: Easy version control with renv

RStudio projects make it super easy to version control your packages using the renv package. That way you can be more confident that when you open up your project in two years, all of the packages that you used are documented with version numbers and therefore your code should still run.

This is especially important if you use R in a company that uses the same scripts over and over again. You really want to avoid that the next person who’s working with your script has to figure out what package versions these scripts need in order to run properly.

In short, combining RStudio projects and renv is a huge productivity boost for everyone. And now that you know all the cool advantages let me tell you how to set up RStudio projects and renv.

How to set up RStudio projects

In RStudio, all you have to do is to click on the Project widget in the top-right corner of RStudio and hit the “New Project” option.

In there, you can just follow the instructions to set up a new project and put it in the directory that you want to use. If you have no particular purpose in mind, you can just create a regular project. For other use cases, RStudio also offers alternatives that are RStudio projects as well but come with a bit of extra content.

Once you have created the project, you can open it either by clicking on the .Rproj file in your project directory or by clicking on the list within the navigator in RStudio. In particular, if you want to open a project in a separate RStudio session, you can also click on the window icon.

How to set up renv

And if you want to use renv, you can just click on the checkbox that says “Use renv with this project” during the project creation process

The only thing you have to remember is that in an renv project, it is as if you haven’t installed any packages on your computer. You basically start this project with a clean slate. Outside of that project, you still have all the packages that are installed on your computer, but inside there isn’t anything.

So, if you want to use a particular package, you will have to install it. But instead of calling install.packages(), you will have to use renv::install(). Also, you have to remember that you need to document the versions of these packages, and that’s where renv::snapshot() comes in. This will document all the packages that you actually use within your project into a so-called lock file.

This means that if you installed, say, ggplot2, but you don’t actually use this package, then the lock file will not include ggplot2. You see, the lock file is supposed to be a list of the minimal amount of packages that you need to run the code that is inside of your project. That’s why renv will auto-detect what packages you actually use.

So there you have it. That’s how you give yourself a nice productivity boost and make your life easier with version control. As always, I hope you enjoyed this newsletter. And if you have any questions don’t hesitate to reply to this mail or contact me on LinkedIn.

See you next week,
Albert đź‘‹

Enjoyed this newsletter? Here are other ways I can help you:

Join the conversation

or to participate.