[R] Querying the path separator?

Duncan Murdoch murdoch.duncan at gmail.com
Thu Jun 30 21:40:39 CEST 2011


On 30/06/2011 3:32 PM, Jonathan Greenberg wrote:
> Unfortunately, no.  .Platform$file.sep on a windows box is the "/".
> For this DOS program it needs to be the "classic" backslash.
>
> Ok, we'll hack this a bit (query the platform and test if it is
> windows), but now I'm running into escape character problems.  Given:
>
> files1="*.las"
> files1_formatted=file.path(getwd(),files1)
> files1_formatted
> [1] "X:/pathto/*.las"
>
> I want files1_formatted to read:
> "X:\pathto\*.las"
>
> Using:
> sub("/","\",files1_formatted)
> Doesn't work (in fact, its not a complete statement -- I assume since
> its waiting for something to follow the / ).  What's the trick with
> this?

The problem is with the backslash, which is an escape character in R.  
Use this instead:

gsub("/", "\\", files1_formatted, fixed=TRUE)

The "g" in gsub says do them all, the fixed=TRUE says to interpret 
everything as plain characters, not regular expressions (which would 
need extra escaping).  It will display with escaped backslashes, but 
there's really just one in there, as cat() will show.

Duncan Murdoch

> --j
>
>
>
> On Tue, Jun 28, 2011 at 11:05 PM, Berend Hasselman<bhh at xs4all.nl>  wrote:
> >
> >  Jonathan Greenberg wrote:
> >>
> >>  The problem is that I'm trying to create a path to use with a system()
> >>  call, and the command window will not work with the forward slash,
> >>  only with the standard backslash.  I do understand that within R it
> >>  will work with either way, but not via the system call.  I'm trying to
> >>  create a "generic" system() call that will work with an external
> >>  executable that is available on both windows and unix machines.
> >>
> >
> >  Would .Platform$file.sep help you?
> >  I can't test this.
> >
> >  Berend
> >
> >  --
> >  View this message in context: http://r.789695.n4.nabble.com/Querying-the-path-separator-tp3631806p3632161.html
> >  Sent from the R help mailing list archive at Nabble.com.
> >
> >  ______________________________________________
> >  R-help at r-project.org 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.
> >
>
>
>



More information about the R-help mailing list