Paul Roebuck
2004-Jul-12 16:01 UTC
.Platform addition (was Re: [R] where does R search when source()?)
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? ---------------------------------------------------------- SIGSIG -- signature too long (core dumped)
Duncan Murdoch
2004-Jul-12 16:50 UTC
.Platform addition (was Re: [R] where does R search when source()?)
On Mon, 12 Jul 2004 11:01:49 -0500 (CDT), Paul Roebuck <roebuck at odin.mdacc.tmc.edu> wrote :>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?I'm not sure I follow. What would be the difference between .Platform$file.sep and .Platform$path.sep ? By the way, as far as I know .Platform$file.sep is "/" on all platforms now. In Windows R converts these to "\" when necessary. Duncan Murdoch
Gabor Grothendieck
2004-Jul-13 00:13 UTC
.Platform addition (was Re: [R] where does R search when source()?)
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) }