Re: [R] search path question

From: Barry Rowlingson <b.rowlingson_at_lancaster.ac.uk>
Date: Wed, 30 May 2007 10:18:09 +0100

Prof Brian Ripley wrote:

>
> You could do this via a search_file() connection wrapper, but there is a
> problem with ensuring connections get closed (which on.exit does here).
>

  I'm not sure exactly what you mean by a 'search_file() connection wrapper', but I have realised that its probably a better idea to write a function that checks a search path for a file and then returns that file:

search_file =
function(name,path=options()$scanpath,...){ for(p in path){

       file=file.path(p,name)
       if(file.exists(file)){
         return(file)
       }
     }

return(name)
}

  Then you can use that in any filename-using function:

  options(scanpath=c("/data1","/data2","/etc"))

  > search_file("passwd")
  [1] "/etc/passwd"

  > passwd = read.table(search_file("passwd"),sep=":")   > record = scan(search_file("passwd"),what='')[1]

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 Wed 30 May 2007 - 09:19:25 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 Wed 30 May 2007 - 12:31:40 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.