MMEL
MMEL

Reputation: 358

How to use "windows folder path" "c:\bla\bla\" in R script variable without manual editing

I wish to copy and paste the windows full folder path in an R variable such as

wd <- "C:\Users\mike\DATA\Statistics_R_\output_examples"

even IF later on I have to use either sub or gsub to change those backslash into forwardslash.

I can't as it keeps giving me the following error:

Error: '\U' used without hex digits in character string starting ""C:\U"

I found that this gsub 'coding' will work: gsub(pattern="\\\\", replacement="/",wd)

BUT first, you have to manually change the path to add another backslash? Doesn't this defeat the purpose of using these sub/gsub functions?

So this would work:

wd <- "C:\\Users\\mike\\DATA\\Statistics_R_\\output_examples"
gsub(pattern="\\\\", replacement="/",wd)

BUT this would not :

wd <- "C:\Users\mike\\DATA\Statistics_R_\output_examples"
gsub(pattern="\\", replacement="/",wd)

Maybe there is not a way to prevent R from interpreting a backslash, even if that backslash is inside a string?

Upvotes: 2

Views: 192

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269852

If C:\Users\mike\DATA\Statistics_R_\output_examples is on the clipboard then either of these will read it into wd:

wd <- readLines("clipboard")

wd <- readClipboard()

giving:

> wd
[1] "C:\\Users\\mike\\DATA\\Statistics_R_\\output_examples"

Whether you even need to change the backslashes depends on what you want to do with it. You may not need to. If you do then this would do it:

chartr("\\", "/", wd)

Upvotes: 4

Related Questions