Re: [R] search path question

From: Barry Rowlingson <b.rowlingson_at_lancaster.ac.uk>
Date: Tue, 29 May 2007 18:34:06 +0100

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



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 29 May 2007 - 17:33:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 May 2007 - 18:31:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.