0

I have written a short script as an R-executable. How can I define the working directory the folder in which the script file is?

Cannot find the C# analogy of

 path = Directory.GetCurrentDirectory() 

Thank you,

Arkadi w
  • 129
  • 22
  • `getwd()` and `setwd()`? – user2974951 Dec 10 '18 at 10:46
  • Have a look at library([here](https://github.com/r-lib/here)) which constructs relative paths to your project root. [Here](https://www.tidyverse.org/articles/2017/12/workflow-vs-script/) you can find some information why this should be the preferred way over `setwd()`. – ismirsehregal Dec 10 '18 at 10:54
  • @ismirsehregal That package is explicitly *not* suited for script usage, only for interactive sessions. – Konrad Rudolph Dec 10 '18 at 11:30
  • Yes, you are absolutly right - The library won't be helpful when writing standalone scripts or packages. Maybe this is something which should also be pointed out in one of the [answers](https://stackoverflow.com/a/48119985/9841389) to the origional question. Still I think it is worth mentioning in this subject area since it provides a suitable workaround for smaller projects during the development phase. – ismirsehregal Dec 10 '18 at 12:52

1 Answers1

1

It seems like this is a duplicate question, where a useful answer was provided.

By using the commandArgs(trailingOnly = FALSE) option, it is possible to return the whole set of arguments passed (including the executable and the filename, not just the arguments for the script). This will include the origin file provided when running Rscript file.R (for example).

  • I saw that thread of 2009 actually. I was wondering if something easier hasn't been developed since for such a necessary and easy task. Thank you, let me try. – Arkadi w Dec 10 '18 at 13:06