RStudio Projects will make your life easier
Have you ever wanted to work on two (or even more) different, separate things in R, at the same time? Do you crave some order and organization in your R life? Yes? Then you need to start using RStudio projects!
(Side note: I assume that this is all pretty much the same on Windows as it is on Mac OS, but because using Windows makes me want to remove my own brain with a butter knife, I’m not sure… proceed with caution, Windows users.)
What is an “RStudio project”?
A project is just a directory, with an .RProj file in it. The .RProj file is basically an RStudio workspace and the working directory is automatically the folder in which it is located. So, when you open an .RProj file, you’ll see an RStudio window open up, and it will be it’s own work environment, and the working directory will automatically be the folder in which it is located.
Why is that cool?
There are two major reasons (for me at least, maybe you’ll have your own different reasons too) why RStudio projects are awesome:
- You can have more than one open at a time! Each .RProj window will have it’s own, independent workspace (by this, I mean that objects and processes in one project are independent of the other) - so you can run different things, have different scripts open, and have different objects in each open project. Maybe you are running a slow process for one analysis, and you want to wrangle some other data for a different analysis while you wait. Or maybe you want to just quickly check some output in another analysis, eventhough you’re still working on this new one? No problem! Check the other one without interfering at all with the new one! Big presentation coming up and you need to synthesize the results of a bunch of different analyses and you have lots of plots you want to tweek and results to check and you want to be able to access all of them at any time? No problem! You can have everything open at once!
- It’s super easy to share and collaborate! If you organize your files nicely - with all of the input/raw data and scripts in the project folder - then if you need to share your work, you just zip the project folder and send it off and it’s all ready to go! No need to scroll around looking for the right input data and the correct scripts! No need to include
rm(list = ls(all.names = TRUE))and
setwd()at the start of every script! No need for your collaborators to carefully go through your code, looking for loaded data/objects, and make sure they change all the file paths correctly! Your collaborator can just unzip the file, save it anywhere on their own drive, and run your scripts as is.
Wait, what was that about the working directory?
So, if you’re used to starting your scripts by clearing the workspace environment (
rm(list = ls(all.names = TRUE))) and then setting the working directory (
setwd()), you won’t need to do that anymore with projects!
Put all of the files that you need into the same folder as (or subfolders within) the project folder. Then, you can just load them (using whatever function you need; ex.
read.csv, etc) by referring only to the path within the project folder itself.
So, for example, in each of my project folders, I usually have: the scipts for that analysis (ordered in such a way that makes sense for that workflow), a ‘raw data’ folder (with .xlsx, .csv, or .Rdata files), a ‘plots’ folder (with the output plots/maps/data viz), and a ‘prepped data’ folder (where the wrangled/clean/output-data/data-for-the-subsequent-analysis goes). And so, in my scripts, the path names needed for loaded or saving data just start with such folder names, ex.
load("raw data/input-data-for-this-analysis.Rdata"). Maybe the photos below help to make this make sense…
Ok, so how do I make a project?
And then go to this link for more details on making and using RStudio projects! It feels inefficient (or I’m just lazy / it’s almost 1am and I’m tired) to rephrase and rewrite everything when that website has such thorough and clear instructions!
I hope using RStudio projects will make your life as much easier as it did mine. Enjoy!