Outsource function/script permanently

Viewed 61

In every Script I paste the same path to get some functions/r-scripts. Is there a possibility to save this path globally and permanently? It should be independently of the workingspace or other loading data paths and should be available on every new R-Session.

Thanks.

1 Answers

1) Place this in your .Rprofile file. The messsage line is optional but will allow you to confirm that .Rprofile was run when you start R.

message("Hello")
options("mypath" = "...path goes here...")

and then in your script get it using the following which will use that path if the mypath option was defined or the current directory otherwise. Omit the second argument if you don't want to have a default.

getOption("mypath", ".")

2) You could also just put this in your .Rprofile

mypath <- "...your path goes here..."

and then just refer to mypath in the script but that will cause mypath to clutter up your workspace which you may not want.

3) R also will read the environment variables defined in .Renviron so if that file contained:

MYPATH="...path goes here..."

then the script could get it via Sys.getenv("MYPATH") .

See ?Startup for more information on .Rprofile and .Renviron .

4) You could also create an exported shell variable on Linux or environment variable on Windows and then read it in the script using Sys.getenv .

Related