Ignore escape characters in a string...

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

Ignore escape characters in a string...

Jonathan Greenberg-4
R-helpers:

One of the minor irritations I have is copying paths from Windows
explorer, which look like:

C:\Program Files\R\R-3.0.3

and using them in a setwd() statement, since the "\" is, of course,
interpreted as an escape character.  I have to, at present, manually
add in the double slashes or reverse them.

So, I'd like to write a quick function that takes this path:

winpath <- "C:\Program Files\R\R-3.0.3"

and converts it to a ready-to-go R path -- is there a way to have R
IGNORE escape characters in a character vector?

Alternatively, is there some trick to using a copy/paste from Windows
explorer I'm not aware of?

--j



--
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
259 Computing Applications Building, MC-150
605 East Springfield Avenue
Champaign, IL  61820-6371
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: [hidden email], Gchat: jgrn307, Skype: jgrn3007

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ignore escape characters in a string...

barry rowlingson
On Tue, Apr 8, 2014 at 4:00 PM, Jonathan Greenberg <[hidden email]>wrote:

> C:\Program Files\R\R-3.0.3


Does R on windows have clipboard support? I can do this on Linux:

 > readLines(file("clipboard"))
[1] "C:\\Program Files\\R\\R-3.0.3"

- that's from a copy of a path with only single slashes in. But
help(connections) on my linux system doesn't mention the Windows
clipboard....

Some ppl on SO have assorted solutions involving Windows scripting tools
that tweak the clipboard so you can Ctrl-V a modified value:

http://stackoverflow.com/questions/1407238/relief-from-backslash-irritation-in-r-for-windows

Barry

        [[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ignore escape characters in a string...

Frede Aakmann Tøgersen-2
In reply to this post by Jonathan Greenberg-4
Yes R on Windows have something like that you mentioned. I have only tried it in conjunction with read.table.

I'm a bit surprised about the \\ on a linux OS. I'm also surprised that in a file manager on Windows you can paste e.g. C:/users/frtog/Desktop and it can find its way to the folder. Weird.

Br. Frede


Sendt fra Samsung mobil


-------- Oprindelig meddelelse --------
Fra: Barry Rowlingson
Dato:08/04/2014 17.20 (GMT+01:00)
Til: Jonathan Greenberg
Cc: r-help
Emne: Re: [R] Ignore escape characters in a string...

On Tue, Apr 8, 2014 at 4:00 PM, Jonathan Greenberg <[hidden email]>wrote:

> C:\Program Files\R\R-3.0.3


Does R on windows have clipboard support? I can do this on Linux:

 > readLines(file("clipboard"))
[1] "C:\\Program Files\\R\\R-3.0.3"

- that's from a copy of a path with only single slashes in. But
help(connections) on my linux system doesn't mention the Windows
clipboard....

Some ppl on SO have assorted solutions involving Windows scripting tools
that tweak the clipboard so you can Ctrl-V a modified value:

http://stackoverflow.com/questions/1407238/relief-from-backslash-irritation-in-r-for-windows

Barry

        [[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
and provide commented, minimal, self-contained, reproducible code.

        [[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ignore escape characters in a string...

Rui Barradas
In reply to this post by barry rowlingson
Hello,

There is support for the clipboard on Windows 7.
Also, note that on Windows your solution leaves a connection open so
maybe the following is better.

# copy the next line
C:\Program Files\R\R-3.0.3

clipb <- file("clipboard")
winpath <- readLines(clipb)
close(clipb)


And, just to avoid backslashes altogether,

fun <- function(x) gsub("\\\\", "/", x)
fun(winpath)

Hope this helps,

Rui Barradas

Em 08-04-2014 16:17, Barry Rowlingson escreveu:

> On Tue, Apr 8, 2014 at 4:00 PM, Jonathan Greenberg <[hidden email]>wrote:
>
>> C:\Program Files\R\R-3.0.3
>
>
> Does R on windows have clipboard support? I can do this on Linux:
>
>   > readLines(file("clipboard"))
> [1] "C:\\Program Files\\R\\R-3.0.3"
>
> - that's from a copy of a path with only single slashes in. But
> help(connections) on my linux system doesn't mention the Windows
> clipboard....
>
> Some ppl on SO have assorted solutions involving Windows scripting tools
> that tweak the clipboard so you can Ctrl-V a modified value:
>
> http://stackoverflow.com/questions/1407238/relief-from-backslash-irritation-in-r-for-windows
>
> Barry
>
> [[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
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ignore escape characters in a string...

barry rowlingson
In reply to this post by Jonathan Greenberg-4
On Tue, Apr 8, 2014 at 4:30 PM, Frede Aakmann Tøgersen <[hidden email]>wrote:

>
> I'm a bit surprised about the \\ on a linux OS. I'm also surprised that in
> a file manager on Windows you can paste e.g. C:/users/frtog/Desktop and it
> can find its way to the folder. Weird.
>
>
 Well, the clipboard contained a path separated by single backslashes. R
read that (via the file("clipboard")) and correctly read single
backslashes. Its only when printed out that R 'escapes' them as
double-backslashes - there's really only one character there.

 What R seems to lack is 'raw string'  functionality. In python you do this
with a string quote prefix, for example 'r':

 >>> len("\t")
 1

 In that case the \t is interpreted as a single character, \t, or tab. Add
the "r" modifier:

>>> len(r"\t")
2

and now you get two characters, backslash and t.

You'll often see regular expressions in Python using raw strings since they
tend to contain slashes and backslashes which you really want in there.

Barry

        [[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ignore escape characters in a string...

Frede Aakmann Tøgersen-2
Thank you Barry, this explains some of the things going on on the different platforms. One of my colleague told me some time ago that in C#/.NET they have this "raw string" functionality as you call it. However there they use @ and not r. That could be a nice thing to have in R.

Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
[hidden email]<mailto:[hidden email]>
http://www.vestas.com<http://www.vestas.com/>

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.

From: [hidden email] [mailto:[hidden email]] On Behalf Of Barry Rowlingson
Sent: 8. april 2014 17:40
To: Frede Aakmann Tøgersen
Cc: Jonathan Greenberg; r-help
Subject: Re: [R] Ignore escape characters in a string...



On Tue, Apr 8, 2014 at 4:30 PM, Frede Aakmann Tøgersen <[hidden email]<mailto:[hidden email]>> wrote:

I'm a bit surprised about the \\ on a linux OS. I'm also surprised that in a file manager on Windows you can paste e.g. C:/users/frtog/Desktop and it can find its way to the folder. Weird.


 Well, the clipboard contained a path separated by single backslashes. R read that (via the file("clipboard")) and correctly read single backslashes. Its only when printed out that R 'escapes' them as double-backslashes - there's really only one character there.
 What R seems to lack is 'raw string'  functionality. In python you do this with a string quote prefix, for example 'r':

 >>> len("\t")
 1
 In that case the \t is interpreted as a single character, \t, or tab. Add the "r" modifier:

>>> len(r"\t")
2
and now you get two characters, backslash and t.
You'll often see regular expressions in Python using raw strings since they tend to contain slashes and backslashes which you really want in there.
Barry


        [[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ignore escape characters in a string...

Jeff Newmiller
In reply to this post by Jonathan Greenberg-4
What is wrong with

winpath <- readLines("clipboard ")

?

If you want to show that as a literal in your code, then don't bother assigning it to a variable, but let it echo to output and copy THAT and put it in your source code.

There is also file.choose()...

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On April 8, 2014 8:00:03 AM PDT, Jonathan Greenberg <[hidden email]> wrote:

>R-helpers:
>
>One of the minor irritations I have is copying paths from Windows
>explorer, which look like:
>
>C:\Program Files\R\R-3.0.3
>
>and using them in a setwd() statement, since the "\" is, of course,
>interpreted as an escape character.  I have to, at present, manually
>add in the double slashes or reverse them.
>
>So, I'd like to write a quick function that takes this path:
>
>winpath <- "C:\Program Files\R\R-3.0.3"
>
>and converts it to a ready-to-go R path -- is there a way to have R
>IGNORE escape characters in a character vector?
>
>Alternatively, is there some trick to using a copy/paste from Windows
>explorer I'm not aware of?
>
>--j

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ignore escape characters in a string...

Jonathan Greenberg-4
Thanks all, I'll try some of these suggestions out but it seems like a
raw string ability could come in helpful -- there aren't any packages
out there that have this capability?

--j

On Tue, Apr 8, 2014 at 1:23 PM, Jeff Newmiller <[hidden email]> wrote:

> What is wrong with
>
> winpath <- readLines("clipboard ")
>
> ?
>
> If you want to show that as a literal in your code, then don't bother assigning it to a variable, but let it echo to output and copy THAT and put it in your source code.
>
> There is also file.choose()...
>
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> On April 8, 2014 8:00:03 AM PDT, Jonathan Greenberg <[hidden email]> wrote:
>>R-helpers:
>>
>>One of the minor irritations I have is copying paths from Windows
>>explorer, which look like:
>>
>>C:\Program Files\R\R-3.0.3
>>
>>and using them in a setwd() statement, since the "\" is, of course,
>>interpreted as an escape character.  I have to, at present, manually
>>add in the double slashes or reverse them.
>>
>>So, I'd like to write a quick function that takes this path:
>>
>>winpath <- "C:\Program Files\R\R-3.0.3"
>>
>>and converts it to a ready-to-go R path -- is there a way to have R
>>IGNORE escape characters in a character vector?
>>
>>Alternatively, is there some trick to using a copy/paste from Windows
>>explorer I'm not aware of?
>>
>>--j
>



--
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
259 Computing Applications Building, MC-150
605 East Springfield Avenue
Champaign, IL  61820-6371
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: [hidden email], Gchat: jgrn307, Skype: jgrn3007

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.