AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
![]() The RStudio project file is a file that sits in the root directory, with the extension. RStudio projects solve the problem of ‘fragile’ file paths by making file paths relative. So I’m totally with Jenny Bryan on this one! 2Įasy file path referencing with RStudio projects (Check out this link for the original blog post)Īt the beginning I was sceptical about the seemingly radical move of abandoning the setwd() orthodox entirely, but since I’ve tried out the project workflow I’ve never really thought about using absolute file paths again. ![]() When you do this for the 73rd time in 2 days, while marking an assignment, you start to fantasize about lighting the perpetrator’s computer on fire. To recreate and perhaps extend this plot, the lucky recipient will need to hand edit one or more paths to reflect where the project has landed on their machine. The project is not self-contained and portable. It’s also unlikely to work for the author one or two years or computers from now. The chance of the setwd() command having the desired effect – making the file paths work – for anyone besides its author is 0%. 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. 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.
0 Comments
Read More
Leave a Reply. |