gsub("\\", "\\\\", "C:\Program Files\R")

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

gsub("\\", "\\\\", "C:\Program Files\R")

mafia88
Hi,

I think it is not possible in R but I rather ask before giving up:

What I have: I have copied "C:\Program Files\R" into my clipboard.
What I want: setwd(transform("C:\Program Files\R")) where the function transform should change the "C:\Program Files\R" from my clipboard to "C:\\Program Files\\R" so that R can handle it. Is this possible?
I've tried with gsub("\\", "\\\\", "C:\Program Files\R") but failed, then tried first to split the full clipboard string cause \P is reserved in R but with strsplit("C:\Program Files\R", "") I also failed.

Therefore the question: It is really necessary to manually add a second "\" in "C:\Program Files\R", so that setwd("C:\\Program Files\\R") can change my work directory?
Cause I am getting tired making this manually... yes I know I can use the Search&Replace function from the Editor but I thought R can do everything?

I hope someone could help me :(
Reply | Threaded
Open this post in threaded view
|

Re: gsub("\\", "\\\\", "C:\Program Files\R")

Uwe Ligges-3


On 12.08.2011 10:52, mafia88 wrote:

> Hi,
>
> I think it is not possible in R but I rather ask before giving up:
>
> What I have: I have copied "C:\Program Files\R" into my clipboard.
> What I want: setwd(transform("C:\Program Files\R")) where the function
> transform should change the "C:\Program Files\R" from my clipboard to
> "C:\\Program Files\\R" so that R can handle it. Is this possible?
> I've tried with gsub("\\", "\\\\", "C:\Program Files\R") but failed, then
> tried first to split the full clipboard string cause \P is reserved in R but
> with strsplit("C:\Program Files\R", "") I also failed.
>
> Therefore the question: It is really necessary to manually add a second "\"
> in "C:\Program Files\R", so that setwd("C:\\Program Files\\R") can change my
> work directory?
> Cause I am getting tired making this manually... yes I know I can use the
> Search&Replace function from the Editor but I thought R can do everything?
>
> I hope someone could help me :(


Not really: You need to get it "transformed" before the R parser has its
hand on it, therefore the best solution is to ask your editor to do that.

Uwe Ligges




> --
> View this message in context: http://r.789695.n4.nabble.com/gsub-C-Program-Files-R-tp3738251p3738251.html
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: gsub("\\", "\\\\", "C:\Program Files\R")

ghostwheel
In reply to this post by mafia88
Interesting question. It is really about how to avoid the interpretation of \ as an escape character in a string. I wonder if it is possible.

Anyway, have you tried the following:
setwd(readLines("clipboard",warn=F))

"clipboard" is a special filename that tells R to read from the clipboard.

Michael

On 12 Aug 2011, at 10:52AM, mafia88 wrote:

> Hi,
>
> I think it is not possible in R but I rather ask before giving up:
>
> What I have: I have copied "C:\Program Files\R" into my clipboard.
> What I want: setwd(transform("C:\Program Files\R")) where the function
> transform should change the "C:\Program Files\R" from my clipboard to
> "C:\\Program Files\\R" so that R can handle it. Is this possible?
> I've tried with gsub("\\", "\\\\", "C:\Program Files\R") but failed, then
> tried first to split the full clipboard string cause \P is reserved in R but
> with strsplit("C:\Program Files\R", "") I also failed.
>
> Therefore the question: It is really necessary to manually add a second "\"
> in "C:\Program Files\R", so that setwd("C:\\Program Files\\R") can change my
> work directory?
> Cause I am getting tired making this manually... yes I know I can use the
> Search&Replace function from the Editor but I thought R can do everything?
>
> I hope someone could help me :(
>
> --
> View this message in context: http://r.789695.n4.nabble.com/gsub-C-Program-Files-R-tp3738251p3738251.html
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: gsub("\\", "\\\\", "C:\Program Files\R")

Duncan Murdoch-2
In reply to this post by mafia88
On 11-08-12 4:52 AM, mafia88 wrote:
> Hi,
>
> I think it is not possible in R but I rather ask before giving up:
>
> What I have: I have copied "C:\Program Files\R" into my clipboard.
> What I want: setwd(transform("C:\Program Files\R")) where the function
> transform should change the "C:\Program Files\R" from my clipboard to
> "C:\\Program Files\\R" so that R can handle it. Is this possible?

No, but you shouldn't be doing that.  You should read the string from
the clipboard as a string, not as R code, because it's a string, not R code.

For example, if your clipboard contains that string,

dir <- readClipboard()
setwd(dir)


> I've tried with gsub("\\", "\\\\", "C:\Program Files\R") but failed, then
> tried first to split the full clipboard string cause \P is reserved in R but
> with strsplit("C:\Program Files\R", "") I also failed.
>
> Therefore the question: It is really necessary to manually add a second "\"
> in "C:\Program Files\R", so that setwd("C:\\Program Files\\R") can change my
> work directory?

No.  You only want a single backslash in the string.  The problem is
that you are using the parser:  to get the parser to create a string
with a single backslash in it, you need to give two to the parser.

So don't use the parser.

Duncan Murdoch

> Cause I am getting tired making this manually... yes I know I can use the
> Search&Replace function from the Editor but I thought R can do everything?
>
> I hope someone could help me :(
>
> --
> View this message in context: http://r.789695.n4.nabble.com/gsub-C-Program-Files-R-tp3738251p3738251.html
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel