Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 923
erikfas Avatar asked Mar 15 '17 16:03

erikfas


People also ask

What does source () in R do?

source causes R to accept its input from the named file or URL or connection or expressions directly. Input is read and parse d from that file until the end of the file is reached, then the parsed expressions are evaluated sequentially in the chosen environment.

How do I create a path to a file in R?

You can use getwd() to obtain the current working directory. To change the directory, use setwd("/path/to/your/working/directory") . If you have a permanent path in which the file functions. r will always be present, then you can use source("/path/to/permanent/directory/functions.

How do I find a file path in R?

If we want to check the current directory of the R script, we can use getwd( ) function. For getwd( ), no need to pass any parameters. If we run this function we will get the current working directory or current path of the R script.


2 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'))
like image 111
Luis Avatar answered Sep 20 '22 20:09

Luis


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.

like image 33
MrFlick Avatar answered Sep 20 '22 20:09

MrFlick