R projects and odd socks: more alike than you think
- Neil Cunningham

- Jul 1, 2020
- 4 min read

Entropy has been described as the tendency for things left unattended to descend into chaos. It’s a pervasive force in our universe. Don’t believe me? Stop getting haircuts or servicing your car, and watch what happens. Or, go and check your sock drawer – damn those infernal odd socks!
R is one of the most popular tools in the data universe. However, If you don’t invest a little bit of energy into organising your work in R pretty soon, entropy is going to take over and your life in R is going to resemble my sock drawer. Trust me, you don’t want that. The good news is that you only need to invest a little energy now and you’ve set yourself up so things will run a lot more smoothly, with a lot less effort. It’ll be like being able to find every email you ever sent or received at the press of a button. OK, not that good – but pretty good, I promise.
How you begin isn’t how you should continue.
When you first start R most people tend to agree that getting started, is the best way to, well… get started. Chances are no-one spent much time explaining how to organise your work, and pretty quickly you were entering code, seeing some small outputs and hopefully creating an eye-poppingly clever chart in ggplot2. Like this one;
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))

One problem with following all the default settings on install is that all files you use or generate with R will simply be saved down to the generic ‘Documents’ folder on your PC. It’s like throwing every sock you ever used in your life haphazardly into a single sock drawer. Then, 35 years later your rushing out the door for that life changing interview and you can’t find a single matched pair of servicable socks.
The important bit of R housekeeping you probably weren’t shown and which will fix this fast, is to use R Projects from now on.
Organising your R work into ‘Projects’.
Every time you start a new chunk of work in R. Create an R Project and it will house all your R files in one easy to find place. Also, the work from this project won’t get muddled up and confused with the work of other projects. Here’s how to do it;
Step 1.
Open R Studio and from the main menu select File > New Project. Or, at the top right of the screen navigate to New Project.

Step 2.
Then select New Directory from the resulting pop-up window.

Step 3.
Then select New Project from the following pop-up window.

Step 4.
Next, give the directory a name (think of a directory as akin to a Folder within a File Explorer window on your PC). In this example my project is about Cars so I have called it ‘Cars RProj’. Notice how I end the name of this Directory with ‘RProj’ – I recommend you do the same and I’ll explain why in Step 6.
Also, click Browse and choose a sub-directory (i.e. folder) from File Explorer where you would like to store this R Project. Notice how I have used a folder I set up previously in File Explorer with the same prefix ‘Cars’ – Again, I suggest you do the same and explainations follow in Step 6.
Then click the Create Project button.

Step 5.
Notice how the Working Directory of this Session of R Studio is now automatically aligned to where you want to save all your R files – in the CarsRProj folder.
Also that ‘Cars Proj’ is the identified R Project in the top right of the window, and a ‘Cars RProj.RProj’ file has been created and is listed amongst the files in the Working Directory – bottom right.
This seamless setup and alignment means all R files you save in this project automatically save to this unique project folder. This prevents confusion, eliminates mixed up Working Directories, and greatly reduces lost work, wasted time and confusing errors.

Step 6.
Notice in the below screenshot, because of the way you named your folders in Step 4, that you now have a ‘Cars’ folder where you can store non R files related to this project (e.g. PDF or MS Word) but not used in R or generated by R.
Then, nested within that you have a ‘Cars RProj’ folder that contains all the R files for this project (data, script, RMarkdown files etc.). Neat!

Step 7.
Next time you want to work on this Project you can open it a few ways
From File Explorer – double click the ‘Cars RProj’ file

Open R Studio from you PC’s Start Menu. Then switch into R Studio, and from R Studio's main menu select File > Open Project. From there the Open Project window will open and you can Browse to find your RProj file and double-click it.
OR
Click the dropdown box in the top right marked ‘Project (None)’ and select ‘Cars Proj’ from the list of recent R Projects you may have been working on.

Next Steps
Creating a Project like this each time you start a new chunk of work in R is like putting your socks into pairs, and having separate sections for summer or winter socks, dark or light socks and so on. Heaven is an organised sock drawer.
This small investment of time won’t overturn the law of entropy in the universe, but will go a long way to holding the forces of chaos at bay and make your life in R a lot easier.
I recommend Chapter 8 of “R for Data Science” by Wickham and Grolemund here for some other cool tips that’ll save you a lot of time and build on the platform provided by sensible use of R Projects described herein, like;
Saving the code (how you delivered outputs), and the outputs of that code (like visualisations) so you can easily find them next to each other in the R Project folder like a beautiful pair of matched socks; or
Setting up one of R’s global settings so each time you close the Project and Session you’re in, finding all your files will be easy, and you have a clean environment that’s not confused by objects or values from your prior session.





Comments