There must be something that I don't understand about the source()
command in R. I'm still new to it, but I cannot for the life of me understand how it gets its directories from! My problem is this:
I have a wrapper script, wrapper.R
, and a source file containing some functions, functions.R
. Both of these are in the same directory. If I call source('functions.R')
inside the wrapper script, while standing inside the directory where both files are located, everything is fine. However, I want to be able to run my wrapper.R
script from some other directory, i.e. not the one where these script are located. If I run my wrapper for another directory, it doesn't work, and I get a cannot open the file
error.
I googled and found lots of different threads, but this question seemed to be very clear. The way I understand it, the way I'm doing it should work. Clearly, I'm misunderstanding something. My reading of that thread leads me to believe that source()
works on the directory in which the file that calls source()
is located in. My reading also leads me to believe that I should not be using chdir = TRUE
, as I want to keep the advertised relative directory.
Seeing as it doesn't work... what am I misunderstanding? How can I source files in the same directory as my wrapper script when called from somewhere else?
getwd()
and you can reset it withsetwd()
. But you could always just do something like source("c:\...") and that should work. – Swiftsource
the files without actually knowing the directory, and still call the wrapper function from some other directory? – Damron