[R] search path question

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue May 29 19:34:06 CEST 2007


Zhiliang Ma wrote:
>  I want to find a function that can simply add
> "C:\inFiles\" into R's search path, so that we I scan a file R will go to
> all the search paths to find it. In matlab, path(path,"C:\inFiles") will do
> this job, I'm just wondering if there is a similar function in R can do this
> job.

Something like this (not extensively tested):

`sscan` <-
   function(name, path=options()$scanpath,...){

     for(p in path){
       file=file.path(p,name)
       if(file.exists(file)){
         return(scan(file,...))
       }
       ## last resort..
       return(scan(name,...))
     }
   }

Then do:

  options(scanpath="/tmp")

  and then:

  sscan("foo.data")

  will look for /tmp/foo.data first, then if that fails it will do the 
'last resort' which is to look in the current directory.

  My worry is that this will bite you one day - if you have two files 
with the same name, it will get the first one in your scanpath - one day 
this will not be the one you think it is....

  Note this only works with 'scan' - you'll have to do the same thing 
for read.table, source, etc etc if you want them to behave with a search 
path too. Unless there's a lower-level approach. But that really will 
bite you!

Barry


Barry



More information about the R-help mailing list