Skip to content

How to convert backslash to slash?

14 messages · Shengqiao Li, jim holtman, Gabor Grothendieck +2 more

#
This was just posted today:

https://stat.ethz.ch/pipermail/r-help/2008-September/174696.html
On Tue, Sep 23, 2008 at 4:00 PM, Shengqiao Li <shli at stat.wvu.edu> wrote:
#
How to use sub, gsub, etc. to replace "\" in a string to "/"?

For example,convert "C:\foo\bar" to "C:/foo/bar".

Thanks,

Shengqiao Li
#
Except it's a bit tricker because:

s <- "C:\foo\bar"
print(s)

You'd need to work with

encodeString( "C:\foo\bar")

but that won't work in general:

"c:\xjobs"
"c:\"
"c:\help"

Hadley
On Tue, Sep 23, 2008 at 3:00 PM, Shengqiao Li <shli at stat.wvu.edu> wrote:

  
    
#
On 23/09/2008 4:00 PM, Shengqiao Li wrote:
If those are R strings, there are no backslashes in the first one.  It 
has a formfeed and a backspace in it.

Duncan Murdoch
#
On Tue, 23 Sep 2008, Duncan Murdoch wrote:

            
I did notice that this string  was special. It's a legimate R string. If 
"f" and "b" are replaced by "d", it will not. My purpose is to convert a 
Windows file path (eg. copied from Explorer location bar) to a R file path 
through some R function inside R terminal. The "File->Change dir..." takes 
a file path like "C:\Acer", but setwd function will fail.  I guess there 
must be some ways in R to replace a backslash by slash, eg. C:\Acer -> 
C:/Acer. The first problem may be how to pass and save this kind of 
strings. encodeString does not work for this, it will just ignore "\".

Shengqiao Li
#
Shengqiao Li wrote:
I didn't say it was not legitimate, I said that it contains no 
backslashes.  If you replace f or b with d, you do not have a legitimate 
string.
That's not true.  If you enter a backslash in the string, setwd() works 
fine.

Your problem is that you are confusing R source code with the strings 
that it represents.  The R source code for the file path C:\Acer is 
"C:\\Acer".  The
R source code "C:\foo\bar" contains no backslashes, it contains the 
characters C, :, formfeed, o, o,  backspace, a, r.

If you have the string C:\Acer in the Windows clipboard, then you can 
read it from there using readClipboard().  (There are many other ways to 
read the clipboard as well;
using 'clipboard' as a filename generally works.) You can then pass it 
to setwd(), and it will be fine.

Duncan Murdoch
#
On Wed, 24 Sep 2008, Duncan Murdoch wrote:

            
Thank you for your reply. readClipboard is a partial solution to this 
case. More generally, if I want to wrtie a R program in which users are 
asked to input a file path. I want this program to be robust and tolerant, 
that is users can type in C:\Acer or C:/Acer.  What's the way to do this?

Shengqiao Li
#
Shengqiao Li wrote:
Just treat their input as data, not as source code.  Backslashes are not 
special in data.

Duncan Murdoch
#
It probably depends on how you are prompting the user for input.  Here
is what happens with 'readline' and you will see that the string after
input does have the backslashes escaped.
Input File: c:\dir1\dir2\file.name
[1] "c:\\dir1\\dir2\\file.name"
[1] "c:/dir1/dir2/file.name"

        
On Wed, Sep 24, 2008 at 8:53 AM, Shengqiao Li <shli at stat.wvu.edu> wrote:

  
    
#
Your users will have to learn that if they are inputting quoted
strings into R, then, by convention, a backslash is used to 'escape'
certain character sequences and if you want a backslash, you have to
escape it ('\\').  You can also have your users upgrade to a system
that does not use backslashes in its file names, or learn that you can
alway use a virgule (/) in the file names.
On Wed, Sep 24, 2008 at 9:59 AM, Shengqiao Li <shli at stat.wvu.edu> wrote:

  
    
#
On Wed, 24 Sep 2008, jim holtman wrote:

            
Great! Thanks. By the way, it would be nice if R have a third way to quote 
a string and automatically escape the backslashes in memory. For instance:

#not run now
s<- `C:\Acer'

print(s)
[1] "C:\\Acer"
#
On Wed, Sep 24, 2008 at 9:59 AM, Shengqiao Li <shli at stat.wvu.edu> wrote:
I agree that that some sort of facility would be convenient.  Creating
latex output is
another situation where not having to escape backslashes would be convenient.
Other languages do have special purpose constructs for this.
#
And regular expressions, of course.

Hadley
#
On 24/09/2008 10:53 AM, hadley wickham wrote:
There were proposals to do this last year (I think you participated in 
the discussion), but there was no agreement on how to do it.  One was 
triple quotes as in Python, e.g.

'''c:\foo\bar'''

and another was some variation on heredocs from several languages, or 
\verb from TeX.

Duncan Murdoch