.Platform addition (was Re: [R] where does R search when source()?)

Gabor Grothendieck ggrothendieck at myway.com
Tue Jul 13 02:13:49 CEST 2004


Paul Roebuck <roebuck <at> odin.mdacc.tmc.edu> writes:

> 
> On Sun, 11 Jul 2004, Gabor Grothendieck wrote:
> 
> > search.path <-
> > function(fn,
> >          paths = strsplit(Sys.getenv("PATH"), split = ";")[[1]],
> >          fsep = "\\") {
> >     for(d in paths) {
> >         f <- file.path(d, fn, fsep = fsep)
> >         if (file.exists(f))
> >             return(f)
> >     }
> >     return(NULL)
> > }
> >
> > source(search.path("myscript.R"))
> 
> I glanced this and thought this might be handy to keep for
> possible use. To make it less Windows-specific, I was going
> to replace Gabor's fsep default value with '.Platform$file.sep'
> when I noticed that .Platform doesn't have a '$path.sep'
> field. Just missing or available elsewhere?

AFAIK the OS-specific separators are not available in R which is why I 
had tried to localize them into the arg list.  The following seems to work
on Windows.  I don't have access to UNIX so perhaps you could see
if it works there too.  In this version the arg list has been reduced
to two arguments and both separators are localized into the default for the
second:

# search for file in paths
# fn is filename
# paths is a vector of path names, default is constructed from PATH
#
# e.g.: source(search.path("myscript.R"))

search.path <- function(fn, 
     paths = strsplit(chartr("\\", "/", Sys.getenv("PATH")), split = 
                switch(.Platform$OS.type, windows = ";", ":"))[[1]]) {
  for(d in paths) 
     if (file.exists(f <- file.path(d, fn)))
        return(f)
  return(NULL)
}




More information about the R-help mailing list