[R] search path question

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed May 30 11:18:09 CEST 2007


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



More information about the R-help mailing list