R: Source() and Path to Source Files
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...
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?
5 Answers
You can do this using the here package. It uses the "current working directory at the time when the package is loaded". In other words, the directory you start your R session from.
In your case the code would be:
source(here::here('functions.R'))
This will work even if the wrapper script wrapper.R is in a different directory in the project.
If functions.R is in a subdirectory of the project, just add it to the call to here(), to complete the relative path:
source(here::here('subdirectory', 'functions.R'))
If you are distributing a script to colleagues, you should really not be writing a script that sources other scripts. What if you want to rename or move functions.R in the future? What if you need to modify a function in functions.R, but wrapper.R relies on the older version of that function? It's a flimsy solution that will cause headache. I would recommend either of the following instead.
Put everything needed into a single, self-contained script and distribute that.
If you really want to separate code into different files, write a package. Might sound like overkill, but packages can actually be very simple and lightweight. In the simplest form a package is just a directory with a
DESCRIPTIONandNAMESPACEfile along with anR/directory. Hadley breaks this down nicely: .
Maybe you can define a helper function in wrapper.R that will try to load other files from the same directory. For example
source_here <- function(x, ...) {
dir <- "."
if(sys.nframe()>0) {
frame <- sys.frame(1)
if (!is.null(frame$ofile)) {
dir <- dirname(frame$ofile)
}
}
source(file.path(dir, x), ...)
}
Then you would call
# inside wrapper.R
source_here("functions.R")
Then you would just have source wrapper.R and it will look for functions.R in the same directory.
One answer I didn't see yet is to just use absolute paths. When you source("myfunctions.R") it's using the implicit relative path from getwd(). Use the full path to avoid problems when you change working directory.
Though, when sharing the work, the others will have to change all the paths by themselves.
source fundamentally doesn’t support this. The other answers show some workarounds that work in limited cases but all fail in some (common) cases. In particular, chdir = TRUE isn’t a good option as you noted yourself.
A better solution is to box::use from the ‘box’ package. This package allows you to treat R source code as proper modules. One property of this is that modules can load local modules.
Inside your wrapper.R, replace the source call by
box::use(./functions[...])
Or, if you want to export these functions from your wrapper.R module (rather than just using them internally), do the following instead:
#' @export
box::use(./functions[...])
And to load wrapper.R itself, use
box::use(project/wrapper)
Where project is the project name, and needs to correspond to the name of the folder that your wrapper.R script is saved in.
Please consult the Get started vignette for more information on the usage of ‘box’ modules.