[R] path for source()

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Feb 8 10:53:25 CET 2007


I don't think anyone have answered the actual question yet.

The answer is simple: the search path for search is '.', the current 
directory.  Just as it is for almost all the software on your system 
except binaries and package addons (including, e.g. R's search path for 
packages).

But R is a progamming language and it takes less time to add a search than 
to post a message here.  Something like (untested)

search.source <- function(file, path=".", ...)
{
    for(p in path) {
        fp <- file.path(p, f)
        if(file.exists(fp)) return(source(fp, ...))
   }
   stop("file ", sQuote(file), " not found")
}

On Thu, 8 Feb 2007, Wolfgang Raffelsberger wrote:

> Hi,
> an easy way to address this is to change directory within R before
> calling source() :
> setwd("D:/Projects/yourProject")
> source("yourCode.R")
>
> Of course you need to know where your .R files are.
> Using getwd() you can always check where you are and using dir() you can
> check the files in your directory (which you could combine with grep()
> to search for ".R") ..
>
> Wolfgang
>
> colliera at ukzn.ac.za a écrit :
>> hello,
>>
>> i have a couple of .R files distributed about my file system. i 
>> commonly source() these from other files, but i have to include the 
>> full file path. this is not always convenient if you move files around. 
>> is there a way of setting the search path for source()?
>>
>> thanks a lot!
>>
>> cheers,
>> andrew.
>>
>>
>
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list