2

I'm working in Rstudio and have multiple scripts open that have different working directories; however, each working directory exists within a larger folder on my computer (see below). Is it possible to add these scripts to an Rstudio Project without reorganizing all my files and changing each script's working directory?

File structure on computer:

Folder A

~~Folder 1

~~Folder 2

~~Folder 3

Say I have 3 scripts open, each with a working directory of either Folder 1, 2, or 3. Can I create a project that incorporates all three scripts. Say, set working directory to "Folder A"

Thanks much.

  • http://www.rstudio.com/ has a help forum that you can check, but you probably should describe your problem in greater detail there since it is not really clear what you are asking. On our site questions that are not related to statistics are off-topic: http://stats.stackexchange.com/help/on-topic – Tim Feb 24 '15 at 18:38
  • I agree that this question is off-topic on Cross Validated. However, I wouldn't recommend to use RStudio's forum (or, at least rely on it as a main source of answers), since they are rather slow in answering questions and sometimes even ignore (miss) them. Such questions should be asked on StackOverflow (SO) with tags `r` and `rstudio`. I will flag your question for migration and local moderators will help you with that. Don't forget to register on SO to be able to interact there. – Aleksandr Blekh Feb 24 '15 at 18:51

2 Answers2

1

Technically, you can change working directory programmatically within a project, but this is considered a very poor practice and is strongly recommended against. However, you can set working directory at a project's top level (full path to Folder A, in your example) and then refer to scripts and objects, located in Folders 1-3 via corresponding relative paths. For example: "./Folder1/MyScript.R" or "./Folder2/MyData.csv".

Aleksandr Blekh
  • 2,462
  • 4
  • 32
  • 64
  • What's so bad about having `setwd()` & `source()`? – gung - Reinstate Monica Feb 24 '15 at 20:40
  • 2
    @gung: I haven't said anything against `source()`. However, explicit use of `setwd()` is considered a bad practice, hurting the software's _reproducibility_: http://stackoverflow.com/q/13770304/2872891. Having said that, there are rare cases, when (project-wide) single use of `setwd()` makes sense. – Aleksandr Blekh Feb 24 '15 at 21:12
0

It should be possible to create a project in the larger folder. You could even construct a simple master script in Folder A to manage this workflow:

setwd("./Folder 1")
source("scriptx")
setwd("..")

setwd("./Folder 2")
source("scripty")
setwd("..")

setwd("./Folder 3")
source("scriptz")
setwd("..")

Compared to source("Folder 1/scriptx") which runs each script within Folder A the master script would be running each script within it's own folder. Just make sure to use setwd("..") after running code in each folder and you can even run code in between to save output to the main Folder A.

If your workflow always creates folders in this manner I don't see how this would not be reproducible if you used relative paths. Albeit platform dependent, this modified version would create folders on the fly and run scripts kept in Folder A.

system("mkdir Folder_1")
setwd("./Folder_1")
source("../Folder A/scriptx")
setwd("..")

Notice here that when running terminal commands in R, it is recommended avoid spaces in directory or file names.

Tom Kelly
  • 1,458
  • 17
  • 25