R Projects and Files in a 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.