3

I wrote a list of different functions and script and I put them in some subfolders of the working directory so I can divide all my functions in arguments (descriptive statistic, geostatistic, regression....)

When I type source("function_in_subfolder") R tells me that there is no function. I understood that it happens because functions have to stay in the working directory. Is there a way to set also subfolders of the working directory as source for the functions (let's say in a hierarchical way)?

matteo
  • 4,683
  • 9
  • 41
  • 77
  • 1
    Sure, `source("./subdir/function_name.R")`. "." will position the path to current directory, the rest should be self explanatory. – Roman Luštrik Jun 25 '13 at 22:36
  • 1
    Please don't confuse functions (objects created with the `function` keyword within R scripts) with source files (the R scripts themselves). – krlmlr Jun 25 '13 at 23:20

2 Answers2

5

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.

krlmlr
  • 25,056
  • 14
  • 120
  • 217
  • Ok, I understood. Actually the simple way to call functions placed in subdirectories of the working one is to call them by adding the "." in path of the source (source("./subdirectory/funtion_1.R)) – matteo Jun 26 '13 at 07:31
  • @matteo: The `./` does not actually change anything, you can omit it. – krlmlr Jun 26 '13 at 07:51
  • oops, you are right.. sorry if I look so clumsy, but I'm switching alone from matlab to R. In matlab I was used to save functions in subfolders of the main one and when I need them I just had to type the name of the function. In R I have to call them with source("path"). – matteo Jun 26 '13 at 08:06
  • So it sounds like his point about "multiple working directories" is not supported. I cannot add to the "path" several folders so that functions defined in various files being `source`'d into will keep their definitions working from the current working directory much like they would in Matlab via [addpath](http://www.mathworks.com/help/matlab/ref/addpath.html). Sounds like I'm going to have to convert my different folders into packages or something. – jxramos Aug 04 '15 at 20:54
0

One solution is to use list.files to get the full path of your function. for example:

    myfunction.path <- list.files(getwd(),
               recursive=TRUE,full.names=TRUE,
              pattern='^myfunction.R$')

Then you can call it :

   source(myfunction.path)

The recursive call of list.files can be expensive, so maybe you should call it once at the beginning of your analyze for example and store all functions paths in a named list. And BE CAREFUL the result can not be unique if you create 2 sources files withe the same name in 2 differents sub-directories.

agstudy
  • 119,832
  • 17
  • 199
  • 261