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

From: Gabor Grothendieck <ggrothendieck_at_myway.com>
Date: Tue 13 Jul 2004 - 10:13:49 EST

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

R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 13 10:30:16 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:54 EST