
As Jenny Bryan points out, the setwd() approach makes it virtually impossible for anyone else other than the original author of the script, on his or her computer, to make the file paths work: A simple action of moving the entire directory to a different sub-folder or to a different drive will break the links, and your script will not run. The problem with this approach is that since setwd() relies on an absolute file path, this makes the links break very easily, and very difficult to share your analysis with others. You then use getwd() to find out what the current working directory is, and check that your working directory is correctly set. When I first started using R several years ago, the textbook and mainstream approach for setting working directories was to use setwd(), which takes an absolute file path as an input then sets it as the current working directory of the R process.
#R GET FILE PATH HOW TO#
It’s one of the non-code related parts of R programming that I think is extremely helpful to know, and arguably for a learner, even a greater priority than learning how to use GitHub! 1 Setting up a working directory properly also helps build up good habits that are conducive to reproducible analysis. Using RStudio projects eliminates so much of the early-stage hassle and confusion around reading in and exporting data. If you are just starting out in R, my personal advice is that using RStudio projects and structuring working directories are ‘must-knows’. The project directory structure covered in this post is one that I use day-to-day myself, and one that I find the most appropriate for the kind of analysis work that I typically deal with, i.e. data sets loaded into memory, and saved within the working directory itself.

This post provides a basic introduction on how to use RStudio Projects and structure your working directories - which is well worth a read if you are still using setwd() to set your directories!Īlthough the R working directory is quite a basic and reasonably well-covered subject, I felt that it would still be worth sharing my own approach of structuring working directories, as clearly there can be multiple sensible and valid ways of structuring a working directory.
