Re: [R] How to convert "c:\a\b" to "c:/a/b"?

From: Henrik Bengtsson <>
Date: Wed 29 Jun 2005 - 01:37:45 EST

(Ted Harding) wrote:
> On 27-Jun-05 Spencer Graves wrote:

>>        Thanks, Dirk, Gabor, Eric:
>>        You all provided appropriate solutions for the stated problem. 
>>Sadly, I oversimplified the problem I was trying to solve:  I copy a 
>>character string giving a DOS path from MS Windows Explorer into an R 
>>script file, and I get something like the following:
>>        D:\spencerg\statmtds\R\Rnews
>>        I want to be able to use this in R with its non-R meaning,

> e.g., in
>>readLine, count.fields, read.table, etc., after appending a file name. 
>>Your three solutions all work for my oversimplified toy example but are
>>inadequate for the problem I really want to solve.

> The various responses show that solving this problem directly within
> R may be, well, problematic!
> I'm not a perl user, but possibly Spencer Graves's speculation might
> eventually be brought into a straightforward solution.
> Unfortunately, for this query, I'm not a Windows users either, so
> can't be authoritative about how practical the following may be for
> Spencer's problem (and similar).
> In the Unix world, the "string editor" program 'sed' simply mops
> up problems of this kind. For example, I just did:
> echo "D:\spencerg\statmtds\R\Rnews" | sed 's@\\@/@g'
> and got the response:
> D:/spencerg/statmtds/R/Rnews
> Note: the parsing of the 'sed' command is as follows:
> s@x@y@g means substitute "y" for every ("g" = "global") occurrence
> of "x".
> The character to be replaced (x="\") is the escape character so
> needs to be escaped ("\\"); but apart from this it's straightforward.
> The usual separator is "/" instead of "@", but I used "@"
> to simplify things since the substitute itself is y="/".
> An alternative using "/" as separator would be
> echo "D:\spencerg\statmtds\R\Rnews" | sed 's/\\/\//g'
> D:/spencerg/statmtds/R/Rnews
> which is a bit more complicated but still straightforward
> (depending on your eyesight). Here, both x="\" and y="/"
> need to be escaped.
> 'sed', and a lot of other useful stuff, is available as "GNU tools"
> which can be installed on Windows, and allow this kind of thing to
> be very slickly done, but outside of R of course.
> I also make much use of 'awk' (but you can equally use perl) for
> tidying up CSV files exported from Excel, and for all sorts of
> rearrangements and substitutions in data files. While Unix users
> take this sort of thing for granted, I suggest to Windows users
> that it could be well worth installing the GNU tools, since they
> are very useful indeed.
> It's not clear from Spencer's follow-up whether doing this kind
> of preliminary work outside of R, and then bringing the results
> into R (e.g. via the clipboard or a file) is practical in his
> context. If not (i.e. all the work has to be done inside R), then
> of course my sugestion above is not helpful in this case!

The closest you can get to the above using R (and on Windows) is

echo D:/spencerg/statmtds/R/Rnews> tmp.txt echo cat(gsub("\\\\", "/", readLines("tmp.txt"))) | R --slave

I do have a question/request to r-devel: In my example, I ideally would like to be able to start an *.R script or alternatively send R expression at the prompt, and then read data from stdin. Two examples:

echo D:/spencerg/statmtds/R/Rnews | R --slave --code 'cat(gsub("\\\\", "/", readLines("tmp.txt")))'


echo cat(gsub("\\\\", "/", readLines("tmp.txt"))) > main.R echo D:/spencerg/statmtds/R/Rnews | R --slave --file main.R

Note that R CMD BATCH does not provide this. Either you send your R code via standard input or the data, but you cannot send both. If you want to have and dynamic interaction between two applications via stdin and stdout, except from ellaborating with .Rprofile/.First I'm not sure how to start my script in the first place.


> Best wishes,
> Ted.
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <>
> Fax-to-email: +44 (0)870 094 0861
> Date: 28-Jun-05 Time: 13:38:37
> ------------------------------ XFMail ------------------------------
> ______________________________________________
> mailing list
> PLEASE do read the posting guide!
> mailing list PLEASE do read the posting guide! Received on Wed Jun 29 01:42:18 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:06 EST