I think 'package:renv' does a lot of the stuff you mentioned here. I think they accomplished your issue by making an .Rprofile file for each project. The working directory would be set to the root directory of the project, the R profile runs and sets .libPaths() as appropriate, then the scripts can run.
If you don't like that idea, then I don't think you can accomplish what you want in base R. There are packages that can determine the path of the current script. I made a package called 'this.path' that does that, and it works with GUIs RGui for Windows, RStudio, VSCode, Jupyter, Emacs (indev version, soon to be on CRAN), and from a shell (Rscript). The core functions of the package are stable, not so much for others. There are plenty of other packages that offer the same: 'here', 'rprojroot', 'whereami', 'funr', 'scriptName', 'envDocument'. There may be others. I can't confirm their stability but it would seem they are stable. On Thu, Nov 23, 2023 at 2:39 PM Tony Wilkes <tony_a_wil...@outlook.com> wrote: > > Hi everyone, > > I have a question. I hope it's not a stupid question. > > Suppose you'd want to perform version control and project isolation. You'd > create a project folder (let's call it "MyProject"), and place all the R > packages you need for that project inside a subfolder (let's say > "MyProject/MyLibrary"). Now you create and run an R-script in "MyProject". > install.packages(), library(), etc. all have a lib.loc argument to specify > the library path. So one can manually specify the path of your project, and > then you'd have your project isolation and version control fully set-up. > > But if I want to set-up the library path automatically, to make it portable, > I would need to determine the script location. In RStudio I can use the > 'rstudioapi' package, which is very stable, and so does not really require > version control. But for outside R-Studio, I have not found a very stable > package that also works. > I prefer not using external R packages that requires version control (i.e. a > package that changes often-ish): you'd need the package to access the project > library, but the project library to access the package. > > This brings me to my actual question: is it possible to determine the source > file location of an R script outside of R-Studio, without resorting to R > packages ? Or else use an R package that is VERY stable (i.e. doesn't change > every (half) a year, like tidyverse packages tend to do)? commandArgs() used > to contain the script path (apparently), but it doesn't work for me. > > By the way: I wish to get the script path in an interactive session. > > Thank you in advance. > > Kind regards, > > Tony > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-package-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-package-devel ______________________________________________ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel