R Projects and Files in a Project

First, watch the video, then read about the projects and R working directories. The video demonstrates one of the ways you can efficiently manage your files in a project. The discussed file structure will work in many cases but may need to be revised when large data are used and it is impossible or impractical to move the data to the local data folders. Also, the video assumes that each script file (for data loading, cleaning, plotting, etc.) is relatively large; hence it makes sense to keep the code separately so it is more manageable. Suppose each file (for data loading, cleaning, visualizing, and statistical analysis) contains just a few lines. In that case, it might be more practical to keep the codes together in a single script – you are free to decide based on the needs and size of your project.

Workflow: projects

Where does your analysis live?

R has a powerful notion of the working directory. This is where R looks for files you ask it to load and where it will put any files you ask it to save. 

RStudio shows your current working directory at the top of the console:



And you can print this out in R code by running getwd():

getwd()
#> [1] "/Users/hadley/Documents/r4ds/r4ds"


As a beginning R user, it's OK to let your home directory, documents directory, or any other weird directory on your computer as R's working directory. But you're six chapters into this book, and you're no longer a ranked beginner. Very soon now, you should evolve to organizing your analytical projects into directories and, when working on a project, setting R's working directory to the associated directory.

I do not recommend it, but you can also set the working directory from within R:

setwd("/path/to/my/CoolProject")


But you should never do this because there's a better way, a way that also puts you on the path to managing your R work like an expert.