[R] How to convert backslash to slash?

jim holtman jholtman at gmail.com
Wed Sep 24 15:58:42 CEST 2008


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:
>
>> 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.
>>
>>> x <- readline("Input File: ")
>>
>> Input File: c:\dir1\dir2\file.name
>>>
>>> x
>>
>> [1] "c:\\dir1\\dir2\\file.name"
>>>
>>> gsub("\\\\", "/", x)  # notice the double escape
>>
>> [1] "c:/dir1/dir2/file.name"
>>>
>
> 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 8:53 AM, Shengqiao Li <shli at stat.wvu.edu> wrote:
>>>
>>> On Wed, 24 Sep 2008, Duncan Murdoch wrote:
>>>
>>>> Shengqiao Li wrote:
>>>>>
>>>>> On Tue, 23 Sep 2008, Duncan Murdoch wrote:
>>>>>
>>>>>
>>>>>> On 23/09/2008 4:00 PM, Shengqiao Li wrote:
>>>>>>
>>>>>>> How to use sub, gsub, etc. to replace "\" in a string to "/"?
>>>>>>>
>>>>>>> For example,convert "C:\foo\bar" to "C:/foo/bar".
>>>>>>>
>>>>>> If those are R strings, there are no backslashes in the first one.  It
>>>>>> has a formfeed and a backspace in it.
>>>>>>
>>>>>
>>>>> 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.
>>>>
>>>> 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.
>>>>>
>>>>> 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.
>>>>
>>>> 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.
>>>
>>> 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
>>>
>>>>
>>>> Duncan Murdoch
>>>>>
>>>>> 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
>>>>>
>>>>>
>>>>>> Duncan Murdoch
>>>>>>
>>>>>>
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>>
>>>>
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list