The source
function has a chdir
argument which, if set to TRUE
, will set the working directory to that where the script resides. The new work directory is valid for the duration of the execution of the script, after that it is changed back. Assumung the following structure
main.R
one/
script.R
two/
subscript.R
you can call source("one/script.R", chdir=T)
from main.R
and, in script.R
, call source("two/subscript.R", chdir=T)
.
However, by default, R will start its search from the current directory. There is no such thing as a "list of search paths" like, e.g., the PATH
environment variable, although apparently someone attempted to create such a thing. I would strongly advise against attempting to find a script file "anywhere". Instead, indicate precisely which script is to be run at which point. Otherwise, name clashes resulting from simply adding a file to your scripts can lead to unpredictable behavior which is also difficult to debug.