Replacing backslashes with slashes

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Replacing backslashes with slashes

yvonnick noel
Hello,

I've seen this question asked in the archives but no clear reply or solution
provided. So, just to be sure it is not possible in R: Can I replace
backslashes with slashes in a string ?

I am writing a GUI for R with the Rpad library. I have a "browse" button for
data loading and Windows return a path string with backslashes. I need to
convert them into slashes to use the string with read.table.

I would have expected something like:

gsub("\\","\/","c:\My Documents\data.dat")

to work but it does not (incorrect regular expression).

Note that I have no control on the string which is returned from the system (no
such problem under Linux BTW).

Any idea ?

Yvonnick NOEL
U. of Rennes 2
FRANCE

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

Ted.Harding
On 04-Jan-06 yvonnick noel wrote:

> Hello,
>
> I've seen this question asked in the archives but no clear reply or
> solution
> provided. So, just to be sure it is not possible in R: Can I replace
> backslashes with slashes in a string ?
>
> I am writing a GUI for R with the Rpad library. I have a "browse"
> button for
> data loading and Windows return a path string with backslashes. I need
> to
> convert them into slashes to use the string with read.table.
>
> I would have expected something like:
>
> gsub("\\","\/","c:\My Documents\data.dat")
>
> to work but it does not (incorrect regular expression).
>
> Note that I have no control on the string which is returned from the
> system (no
> such problem under Linux BTW).
>
> Any idea ?
>
> Yvonnick NOEL
> U. of Rennes 2
> FRANCE

It would work with

gsub("\\\\","/","c:\\My Documents\\data.dat")
[1] "c:/My Documents/data.dat"

which of course is not your case :(

To see what the real problem is, just enter the string itself:

"c:\My Documents\data.dat"
[1] "c:My Documentsdata.dat"

so 'gsub' is not seeing the "\" in the first place.

I don't know what the solution is (though others might),
but that's the problem!

Good luck,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 04-Jan-06                                       Time: 14:01:07
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

Brian Ripley
In reply to this post by yvonnick noel
On Wed, 4 Jan 2006, yvonnick noel wrote:

> Hello,
>
> I've seen this question asked in the archives but no clear reply or solution
> provided.

I've seen the answer many times: what were you searching on?

> So, just to be sure it is not possible in R: Can I replace
> backslashes with slashes in a string ?

Yes.  The Windows R code does it in several places.

> I am writing a GUI for R with the Rpad library. I have a "browse" button for
> data loading and Windows return a path string with backslashes. I need to
> convert them into slashes to use the string with read.table.

Actually, read.table accepts any valid file path, so I don't see why.

> I would have expected something like:
>
> gsub("\\","\/","c:\My Documents\data.dat")
> to work but it does not (incorrect regular expression).

You need one of

gsub("\\\\","/","c:\\My Documents\\data.dat")
gsub("\\","/","c:\\My Documents\\data.dat", fixed = TRUE)
chartr("\\", "/", "c:\\My Documents\\data.dat")

Note that you need to be careful in certain multi-byte locales to change
characters not bytes, but nowadays (post 2.1.0) R is careful.

> Note that I have no control on the string which is returned from the
> system (no such problem under Linux BTW).

Really?  What happens with file names containing backslashes on Linux?

The string will have embedded backslashes, represented by \\ in quoted
strings in R ...

> Any idea ?
>
> Yvonnick NOEL
> U. of Rennes 2
> FRANCE

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

Elizabeth Lawson-2
In reply to this post by yvonnick noel
Try
   
  > name<-"C:\\myfile.txt"
  > name
[1] "C:\\myfile.txt"
  > gsub("([\\])","\\/",name)
[1] "C:/myfile.txt"

yvonnick noel <[hidden email]> wrote:
  Hello,

I've seen this question asked in the archives but no clear reply or solution
provided. So, just to be sure it is not possible in R: Can I replace
backslashes with slashes in a string ?

I am writing a GUI for R with the Rpad library. I have a "browse" button for
data loading and Windows return a path string with backslashes. I need to
convert them into slashes to use the string with read.table.

I would have expected something like:

gsub("\\","\/","c:\My Documents\data.dat")

to work but it does not (incorrect regular expression).

Note that I have no control on the string which is returned from the system (no
such problem under Linux BTW).

Any idea ?

Yvonnick NOEL
U. of Rennes 2
FRANCE

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
 


                       
---------------------------------

 Ring in the New Year with Photo Calendars. Add photos, events, holidays, whatever.
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

yvonnick noel
In reply to this post by Ted.Harding
> It would work with
>
> gsub("\\\\","/","c:\\My Documents\\data.dat")
> [1] "c:/My Documents/data.dat"
>
> which of course is not your case :(

Absolutely. What I get from the interface is the string "c:\My
Documents\data.dat" and NOT the string "c:\\My Documents\\data.dat". That is
why the solutions previously proposed do not solve my problem (but thank you
for your replies !).

Yvonnick NOEL
U. of Rennes 2
FRANCE

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

yvonnick noel
In reply to this post by Brian Ripley
> You need one of
>
> gsub("\\\\","/","c:\\My Documents\\data.dat")
> gsub("\\","/","c:\\My Documents\\data.dat", fixed = TRUE)
> chartr("\\", "/", "c:\\My Documents\\data.dat")

The string I get is an ASCII string in a web page, through the use of
an <INPUT
type="file" ... > tag (with a "browse" button). This string is caught as is by
R through the Rpad interface (using tcltk as a mini local webserver).

So it is not manually input by the user. As it appears in a textfield on a web
page, I could of course ask the user to change it manually and double the
antislashes. But this is not user-friendly.

>> Note that I have no control on the string which is returned from the
>> system (no such problem under Linux BTW).
>
> Really?  What happens with file names containing backslashes on Linux?

I just meant no conversion was needed under Linux since pathnames use slashes.
That's why I didn't see this problem until I had my student use the graphical
interface under Windows.

Thank you,

Yvonnick.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

Brian Ripley
On Wed, 4 Jan 2006, yvonnick noel wrote:

>> You need one of
>>
>> gsub("\\\\","/","c:\\My Documents\\data.dat")
>> gsub("\\","/","c:\\My Documents\\data.dat", fixed = TRUE)
>> chartr("\\", "/", "c:\\My Documents\\data.dat")
>
> The string I get is an ASCII string in a web page, through the use of an
> <INPUT
> type="file" ... > tag (with a "browse" button). This string is caught as is
> by
> R through the Rpad interface (using tcltk as a mini local webserver).
>
> So it is not manually input by the user. As it appears in a textfield on a
> web
> page, I could of course ask the user to change it manually and double the
> antislashes. But this is not user-friendly.

You really don't understand what I wrote (and it is in many places in the
R documentation).  How are you getting that string into R? It does not
contain doubled backslashes: that is the way C represents backslashes.
For example

> foo <- readLines(n=1)
c:\My Documents\data.dat
> foo
[1] "c:\\My Documents\\data.dat"

So if you are reading the file path into R, you do not need to double the
backslashes, only if you are entering it as a character string within
quotes at the R prompt.

>>> Note that I have no control on the string which is returned from the
>>> system (no such problem under Linux BTW).
>>
>> Really?  What happens with file names containing backslashes on Linux?
>
> I just meant no conversion was needed under Linux since pathnames use
> slashes.

As I did say, they can also use backslashes: it is a perfectly valid
character in a Linux file name.

> That's why I didn't see this problem until I had my student use the graphical
> interface under Windows.
>
> Thank you,
>
> Yvonnick.
>
>
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

yvonnick noel
> You really don't understand what I wrote (and it is in many places in
> the R documentation).  How are you getting that string into R?

OK. A practical example is probably necessary here. I have rewritten a small
Rpad page to show you what I mean. It is copied at the end of this post.

Just save it under your current R directory, under the name, say,
"example.Rpad", and then:

library(Rpad)
Rpad("example.Rpad")

Your browser will open and display the page (I tried it with Mozilla Firefox).
Then browse your disk using the browse button and select any file. Then click
the "Print the name" button.

The full file name is sent to R and internally stored in the R variable
"datafile". The embedded R code is just :

print(datafile)

This is the way I get this string.

Yvonnick.

Here is the script :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
  <head>
    <title>An example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso8859-1">
    <script type="text/javascript" src="editor/Rpad_loader.js">
    </script>
  </head>
  <body>
  <H1>Load data</H1>
  <h3><a name="fil"> From a local or remote file </a></h3>
  <span contenteditable="false">
    <p><input type="file" name="datafile" size="40" class="Rpad_input"
id="datafile" rpad_type="Rstring"></p>
    <P><input type="button" onclick="javascript:top.Rpad_calculate();"
value="Print the name"></p>
  </span>
  <span class="wrapperForHidden" contenteditable="false">
    <div class="Rpad_input" rpad_type="R" style="DISPLAY: none">
      <pre>
        # This is the R code
        if(exists("datafile")){
        print(datafile)
        }
      </pre>
    </div>
  </span>
  <hr>
  </body>
</html>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

Peter Dalgaard
In reply to this post by yvonnick noel
yvonnick noel <[hidden email]> writes:

> > You need one of
> >
> > gsub("\\\\","/","c:\\My Documents\\data.dat")
> > gsub("\\","/","c:\\My Documents\\data.dat", fixed = TRUE)
> > chartr("\\", "/", "c:\\My Documents\\data.dat")
>
> The string I get is an ASCII string in a web page, through the use of
> an <INPUT
> type="file" ... > tag (with a "browse" button). This string is caught as is by
> R through the Rpad interface (using tcltk as a mini local webserver).
>
> So it is not manually input by the user. As it appears in a textfield on a web
> page, I could of course ask the user to change it manually and double the
> antislashes. But this is not user-friendly.

But what does R see? I.e., what is in the string that gets passed to
R, and how does it get passed?

You sound confused (many are) about character escapes: Notice that
"\\" is a string with *one* character in it, namely the backslash.
"\"" has no backslash inside; it contains one double-quote character,
etc. The first backslash is just for representation - it is used to
print or specify a string with a quote, a newline, a bell character,
or the backslash escape character itself.

In general, if you read a string from a Tcl/Tk form or text widget or
whatever using the tcltk package, everything should be OK. However, if
the string is somehow being parsed by R, the *interface* has a problem
and needs to do some escaping first.
 

--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

Ted.Harding
In reply to this post by yvonnick noel
On 04-Jan-06 yvonnick noel wrote:
> [...]
> The string I get is an ASCII string in a web page, through
> the use of an <INPUT> type="file" ... > tag (with a "browse"
> button). This string is caught as is by R through the Rpad
> interface (using tcltk as a mini local webserver).
>
> So it is not manually input by the user.
> [...]

Yvonnik, the only line of approach I can think of at the
moment is to somehow read from the web page into a file
(though perhaps a socket/FIFO might work as well).

For example, if I make a file names.txt with contents

  c:\My Documents\data.dat

(even without quotes) then

  A<-readLines("names.txt",n=1)
  > A
  [1] "c:\\My Documents\\data.dat"

so the result is now in the format such that gsub will work.

E.g.

  gsub("\\\\","/",readLines("names.txt",n=1))
  [1] "c:/My Documents/data.dat"

So now all that would remain is for you to work out how best
to get the filename from the web page into the file which is
read by 'readLines'. One would need more information to work
out how to do that in the context of your usage.

Might reading it from the Windows clipboard work?

Hoping, anyway, that this helps a bit!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 04-Jan-06                                       Time: 18:29:12
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

Seth Falcon-2
In reply to this post by yvonnick noel
Hi Yvonnick,

On  4 Jan 2006, [hidden email] wrote:
> I am writing a GUI for R with the Rpad library. I have a "browse"
> button for data loading and Windows return a path string with
> backslashes. I need to convert them into slashes to use the string
> with read.table.

Can you provide some further detail on how the string is created?  I'm
not familiar with Rpad, but I wonder if the issue isn't at that level.

Note that if you create a string with single '\', R eats them up and
at that point there is nothing you can do:

s <- "x\y\z"
s
[1] "xyz"

Do you have a way of printing your path string?  

+ seth

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

yvonnick noel
In reply to this post by Ted.Harding
Ted,

> For example, if I make a file names.txt with contents
>
>  c:\My Documents\data.dat
>
> (even without quotes) then
>
>  A<-readLines("names.txt",n=1)
>  > A
>  [1] "c:\\My Documents\\data.dat"
>
> so the result is now in the format such that gsub will work.
>
> E.g.
>
>  gsub("\\\\","/",readLines("names.txt",n=1))
>  [1] "c:/My Documents/data.dat"

Yes. I agree that this work. But I am sure you'll agree that it is not
a simple
way to get double backslashes ! But it looks like it is the only way to get it
in R. Jim Holtman also suggested a similar solution with scan().

Probably the simplest way to go is to double the backslashes in javascript,
before the string is passed to R.

> So now all that would remain is for you to work out how best
> to get the filename from the web page into the file which is
> read by 'readLines'. One would need more information to work
> out how to do that in the context of your usage.

This would not be a problem. I could use R functions to do this, since Rpad
allows you to mix R code with standard web scripting.

Thank you.

Yvonnick.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Replacing backslashes with slashes

Duncan Golicher
In reply to this post by yvonnick noel
It  is often convenient to quickly set the working directory to a path
copied onto the windows clipboard. A simple trick I have been using for
a while is along the lines given in the previous posts.

setwd.clip<-function()
{
  options(warn=-1)
  setwd(gsub("\\\\","/",readLines("clipboard")))
  options(warn=0)
  getwd()
}


I load this at the start of every session and then write setwd.clip()
whenever I have a path I want to change to on the clipboard. You can of
course write

setwd(gsub("\\\\","/",readLines("clipboard")))

everytime you need it. Obviously it takes longer and there is the minor
detail that the path read from the clipboard is incomplete (no EOL
marker) which leads to an unnecessary warning.


Dr Duncan Golicher
Ecologia y Sistematica Terrestre
Conservación de la Biodiversidad
El Colegio de la Frontera Sur
San Cristobal de Las Casas,
Chiapas, Mexico

Email: [hidden email]

Tel: 967 674 9000 ext 1310
Fax: 967 678 2322
Celular: 044 9671041021

United Kingdom Skypein; 020 7870 6251
Skype name: duncangolicher
Download Skype from http://www.skype.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Setting the working directory on Windows (was: Replacing backslashes with slashes)

Bert Gunter
In a similar vein, a GUI version is:

setwd(dirname(choose.files()))

This gives you a standard Windows file browser -- you just click on any file
in the directory you want to set. Obviously, dirname(choose.files()) is an
easy interactive way to get directories as strings if you need them. See
also ?basename .


-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
 
 

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Duncan Golicher
> Sent: Thursday, January 05, 2006 10:11 AM
> To: [hidden email]
> Subject: Re: [R] Replacing backslashes with slashes
>
> It  is often convenient to quickly set the working directory
> to a path
> copied onto the windows clipboard. A simple trick I have been
> using for
> a while is along the lines given in the previous posts.
>
> setwd.clip<-function()
> {
>   options(warn=-1)
>   setwd(gsub("\\\\","/",readLines("clipboard")))
>   options(warn=0)
>   getwd()
> }
>
>
> I load this at the start of every session and then write setwd.clip()
> whenever I have a path I want to change to on the clipboard.
> You can of
> course write
>
> setwd(gsub("\\\\","/",readLines("clipboard")))
>
> everytime you need it. Obviously it takes longer and there is
> the minor
> detail that the path read from the clipboard is incomplete (no EOL
> marker) which leads to an unnecessary warning.
>
>
> Dr Duncan Golicher
> Ecologia y Sistematica Terrestre
> Conservación de la Biodiversidad
> El Colegio de la Frontera Sur
> San Cristobal de Las Casas,
> Chiapas, Mexico
>
> Email: [hidden email]
>
> Tel: 967 674 9000 ext 1310
> Fax: 967 678 2322
> Celular: 044 9671041021
>
> United Kingdom Skypein; 020 7870 6251
> Skype name: duncangolicher
> Download Skype from http://www.skype.com
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Setting the working directory on Windows

Duncan Murdoch
On 1/5/2006 2:11 PM, Berton Gunter wrote:
> In a similar vein, a GUI version is:
>
> setwd(dirname(choose.files()))
>
> This gives you a standard Windows file browser -- you just click on any file
> in the directory you want to set. Obviously, dirname(choose.files()) is an
> easy interactive way to get directories as strings if you need them. See
> also ?basename .
>

Brian Ripley added a function choose.dir() to R-devel, which will give
you access to the code in the File|Change dir... menu item in R code.
Won't be out for a few more months, though.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html