4.1 Overview

One of the confusing things that R has is the notion of the working directory, namely the folder it looks for data sources and where by default saves all work. If you wanted to find out what R thinks your working directory is, you can just type getwd(). However, what if you wanted to work on a file that exists in another folder? You can also use setwd() to se your working directory, but I would advise against that. Why? If you set your working directory to something that corresponds to how you have organised your collection of folders in your hard drive, if you send me your code and I try to reproduce it, I will almost likely not have the same folder organisation as you did.

To help with reproducibility, you can think of projects as away to organise your workflow. RStudio Projects are a mechanism for keeping all the files associated with a project together in one place – data, R code, results, figures, reports, etc. Projects provide a simple way to isolate work on a single analysis and to organize files and datasets.

Projects are built in to the RStudio IDE, and are a great tool for reproducible workflow. I strongly suggest that all projects should start by creating an RStudio Project: a folder on your computer that holds all the files relevant to that particular piece of work. This approach ensures that the project can be moved around on your computer or onto other computers and will still work.

You can designate a new or existing folder as an RStudio Project. All this means is that RStudio leaves a .Rproj file in the folder, which is used to store settings specific to that project. When you double-click on a .Rproj file, you open a fresh instance of RStudio with the working directory and file browser pointed at the project folder, so it just makes it easier to find data files, save your work, etc.

You should also start using the here package, rather than setting working directories, etc.