[R-pkg-devel] Question regarding finding the source file location without R-packages outside of R-Studio

Jan van der Laan rhe|p @end|ng |rom eoo@@dd@@n|
Thu Nov 23 21:39:46 CET 2023


Can't/don't you use relative paths?

library(..., lib.loc = "./MyLibrary")

Then your project is perfectly portable. The only thing you need to take 
care of is to run your code from your project directory. In R-studio 
this is easily done by using projects. Outside of R-studio it depends on 
how and with what you are running your code; in general the programme 
you are using to work with your R-files will know where the R-files are. 
In the terminal, for example, you can cd to your project dir and work 
from there.

Jan

On 23-11-2023 20:39, Tony Wilkes 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 using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel



More information about the R-package-devel mailing list