[R] \ escape sequence and windows path

David L Carlson dcarlson at tamu.edu
Tue May 20 19:52:01 CEST 2014


That's the solution I was working on. Design the function (the foo() in your example) to read the clipboard so you do not need to paste anything. That greatly reduces how generally the function can be used since it will fail if there is not a path in the clipboard, but it solves (ameliorates?) the immediate problem:

path.files <- function(...) { list.files(readClipboard(), ...) }

If I copy "C:\Rtools" to the clipboard:

> path.files()
[1] "bin"          "COPYING"      "gcc-4.6.3"    "README.txt"   "Rtools.txt"  
[6] "unins000.dat" "unins000.exe" "VERSION.txt"
> path.files(full.names=TRUE)
[1] "C:\\Rtools/bin"          "C:\\Rtools/COPYING"     
[3] "C:\\Rtools/gcc-4.6.3"    "C:\\Rtools/README.txt"  
[5] "C:\\Rtools/Rtools.txt"   "C:\\Rtools/unins000.dat"
[7] "C:\\Rtools/unins000.exe" "C:\\Rtools/VERSION.txt" 

David C


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bert Gunter
Sent: Tuesday, May 20, 2014 12:15 PM
To: Duncan Murdoch
Cc: r-help at r-project.org
Subject: Re: [R] \ escape sequence and windows path

Duncan:

"...
If you want to solve the problem "I have a pathname in the clipboard,
and want to put it in a string", it's not hard to write
a function that essentially does readLines("clipboard")...."

Does not the Windows version R function readClipboard() do this already?

-- Bert


Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch




On Tue, May 20, 2014 at 10:03 AM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> On 20/05/2014 12:04 PM, Knut Krueger wrote:
>>
>> Am 20.05.2014 17:32, schrieb Bert Gunter:
>>
>> >> paste("a","b",sep="\\") ## "\\" is the escaped single backslash
>> > [1] "a\\b"
>> >> cat(paste("a","b",sep="\\"))
>> > a\b
>> >
>> > Does this help clarify? Or have i misunderstood you?
>>
>> @David and @Bert
>>
>> unfortunately yes. My question is more a system level question as about
>> any converting functions.
>>
>> The question is how to paste a windows path directly into an R script
>> (without readline() or readClipboard() and how could I convert this
>> string that it is usable in R. Means any function which is not using
>> escape characters
>>
>> it seems to be possible for readline() an  readClipboard().
>>
>> I do not want to use y=readline() I do want to use
>> y=foo("c:\foo1\foo2\") but this seems to be impossible because R is
>> interpreting one backslash as escape sequence. So how does readline()
>> and readClipboard() are working? Is there any other callback from utils
>> which is able to deal with this?
>
>
> The difference between the two cases is that you are trying to write
> something in the R language when you write
>
> y=foo("c:\foo1\foo2\")
>
> but that is not a legal R statement, since you never close the opening
> quote.  When using readLines() etc., you're just reading data, you're not
> trying to parse it as R code.
>
> I have no idea what you mean by a "callback from utils".
>
> If you want to solve the problem "I have a pathname in the clipboard, and
> want to put it in a string", it's not hard to write
> a function that essentially does readLines("clipboard").  It's a little
> harder to make that platform-neutral, but it sounds as if you're only
> working on Windows.
>
> If you want to solve the problem, "I want to write some R source code that
> contains a pathname, but I don't want to escape the backslashes", then I
> think you're out of luck.  There are ways to read data inline in your
> source, e.g. you can paste
>
> f <- scan(what="character")
> c:\foo1\foo2\
>
>
> into your console to read x, but that doesn't work with source(), so I'd
> avoid it.
>
> Duncan Murdoch
>
>>
>> I am afraid there is no way.
>>
>> The reason is to prevent errors for beginning windows R user. If the
>> path is long they either forget to convert one backslash or all and then
>> they are frustrated that it is not working. And telling them to use
>> search and replace ... Whatever they would replace it might be worse
>> than before ;-)
>>
>> regards Knut
>>
>> ______________________________________________
>> 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.

______________________________________________
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