Re: [R] search path question

2007-05-30 Thread Barry Rowlingson
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@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.


Re: [R] search path question

2007-05-30 Thread Prof Brian Ripley
I meant essentially the same thing but returning file(fixed_up_name).
You can vectorize this BTW:

search_file - function(name, path=getOption(scanpath))
{
 fp - c(name, file.path(path, name)) # better to use direct name first
 fp - fp[file.exists(fp)]
 if(length(fp)) file(fp[1]) else stop(name,  not found)
}

Shortly things like

 read.table(search_file(passwd))

will close the unnamed connection for you.

On Wed, 30 May 2007, Barry Rowlingson wrote:

 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@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.


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
R-help@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.


[R] search path question

2007-05-29 Thread Zhiliang Ma
Hi R users,

Is there a simple function that can add a folder into current R search path?
For example, suppose my current work directory is D:\work, but my input
files are stored in folder C:\inFiles\,  I know I can change work
directory or add C:\inFiles\ before files name when I scan them, but I
don't want to do that. 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.

Thanks,
zhiliang

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] search path question

2007-05-29 Thread Vladimir Eremeev

Yes, it is.
The original is here
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/92829.html

However, it requires some modifications.
Here they are. Sorry, I can test it only in Windows.

search.source - function(file, path=Sys.getenv(PATH), ...) 
{ 
for(p in strsplit(path,.Platform$path.sep)[[1]]) { 
fp - file.path(p, f) 
if(file.exists(fp)) return(source(fp, ...)) 
   } 
   stop(file , sQuote(file),  not found) 
}

Try also looking here.
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/92821.html


Zhiliang Ma wrote:
 
 Is there a simple function that can add a folder into current R search
 path?
 For example, suppose my current work directory is D:\work, but my input
 files are stored in folder C:\inFiles\,  I know I can change work
 directory or add C:\inFiles\ before files name when I scan them, but I
 don't want to do that. 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.
 

-- 
View this message in context: 
http://www.nabble.com/search-path-question-tf3833821.html#a10855885
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] search path question

2007-05-29 Thread Zhiliang Ma
Hi R users,

Is there a simple function that can add a folder into current R search path?
For example, suppose my current work directory is D:\work, but my input
files are stored in folder C:\inFiles\,  I know I can change work
directory or add C:\inFiles\ before files name when I scan them, but I
don't want to do that. 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.

Thanks,
zhiliang

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] search path question

2007-05-29 Thread Barry Rowlingson
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@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.


Re: [R] search path question

2007-05-29 Thread David Forrest
On Tue, 29 May 2007, Zhiliang Ma wrote:

 Hi R users,

 Is there a simple function that can add a folder into current R search path?

This works for adding libraries to your search path, but I don't think it 
would work for finding data files outside of your getwd() quite as you'd 
like:

.libPaths(c(/home/foo/R/library,.libPaths()))

 For example, suppose my current work directory is D:\work, but my input
 files are stored in folder C:\inFiles\,  I know I can change work
 directory or add C:\inFiles\ before files name when I scan them, but I
 don't want to do that. 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.

 Thanks,
 zhiliang

   [[alternative HTML version deleted]]

 __
 R-help@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.


-- 
  Dr. David Forrest
  [EMAIL PROTECTED](804)684-7900w
  [EMAIL PROTECTED] (804)642-0662h
http://maplepark.com/~drf5n/

__
R-help@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.


Re: [R] search path question

2007-05-29 Thread Zhiliang Ma
Thanks, Barry.
In fact, I have a function just like yours, and I'm looking for a simple
alternative function, which is like path in Matlab.

On 5/29/07, Barry Rowlingson [EMAIL PROTECTED] wrote:

 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


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] search path question

2007-05-29 Thread Barry Rowlingson
Zhiliang Ma wrote:
 Thanks, Barry.
 In fact, I have a function just like yours, and I'm looking for a simple
 alternative function, which is like path in Matlab.

  Dont think it can be done - if you look at the code for 'scan', it 
disappears off into internal() calls to do the business of finding and 
reading a file, so you're going to have trouble changing its behaviour 
in R. You'd have to patch R's C source to implement a search path.

Barry

__
R-help@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.


Re: [R] search path question

2007-05-29 Thread Prof Brian Ripley
Actually, it does

 if (is.character(file))
 if (file == )
 file - stdin()
 else {
 file - file(file, r)
 on.exit(close(file))
 }

so all the searching is done in the file() connection.

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).

On Tue, 29 May 2007, Barry Rowlingson wrote:

 Zhiliang Ma wrote:
 Thanks, Barry.
 In fact, I have a function just like yours, and I'm looking for a simple
 alternative function, which is like path in Matlab.

  Dont think it can be done - if you look at the code for 'scan', it
 disappears off into internal() calls to do the business of finding and
 reading a file, so you're going to have trouble changing its behaviour
 in R. You'd have to patch R's C source to implement a search path.

 Barry

 __
 R-help@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.


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
R-help@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.