94

I am looking for an efficient way to convert back slash to forward slash in R. Sometime I copy the link of the directory in Windows and I get something like this:

C:\Users\jd\Documents\folder\file.txt

How can I quickly change this to C:/Users/jd/Documents/folder/file.txt ? I cannot even read the above expression as character. It throws an error

"\u used without hex digits in character string starting ""C:\u".

I know TAB function in R helps to find the location fast, but was just wondering if there was any other work around. I could change the working directory to the location of folder also. I was just playing around and tried to convert backslash to forward slash and was not straight forward so asked this just because of curiosity.

Henrik
  • 65,555
  • 14
  • 143
  • 159
Jd Baba
  • 5,948
  • 18
  • 62
  • 96
  • 10
    From `R 4.0.0` you can use `r"(...)"` to write a path as raw string constant. See [Escaping backslash (\) in string or paths in R](https://stackoverflow.com/a/63078969/1851712) – Henrik Aug 25 '20 at 09:31
  • I just found your comment after I added a similar solution below. I can't seem to find the documentation on this and `r"[...]"` also works. I was wondering if you could pass it objects as well as strings? – pluke May 11 '22 at 15:02
  • here's the documentation, it doesn't appear to allow variables to be passed: https://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html – pluke May 13 '22 at 08:31
  • 2
    Is there really no way to do this programmatically? – Diego Sep 14 '22 at 14:55
  • Does anyone know if there is a modification that will allow the ```r"(...)"``` this to apply to a string pulled as a pass through argument from an Excel VBA Macro? – pseudorandom Jul 13 '23 at 20:11

11 Answers11

70

In R, you've to escape the \ with \\ So, your path should be:

x <- "C:\\Users\\jd\\Documents\\folder\\file.txt"

To get that, you can do:

x <- readline()

then, at the prompt, paste your unmodified path (CTRL+V then ENTER)

Finally, to change \\ to / everywhere, you could use gsub, once again by escaping the \, but twice, as follows:

gsub("\\\\", "/", x)
# [1] "C:/Users/jd/Documents/folder/file.txt"
flodel
  • 87,577
  • 21
  • 185
  • 223
Arun
  • 116,683
  • 26
  • 284
  • 387
  • 1
    thanks for your answer. The only reason I want to change that backslash expression to forward slash is because in windows when I try to copy the address of particular forward it has only one backslash. Now, manually adding backslash will not be efficient. Now, I understand copying simply doesn't work. – Jd Baba Jul 12 '13 at 00:15
  • 25
    `readClipboard()` is another option that will automatically convert the backslashes in copied text from one to two. – thelatemail Jul 12 '13 at 00:40
  • 1
    @thelatemail Thanks, it seems `readClipboard()` is the one that I am looking for. – Jd Baba Jul 12 '13 at 00:45
  • 3
    On my machine, the path is copied with surrounding double quotes: `"C:\Users\...\file.txt"`. When I read this in with `readline()`, the surrounding quotes are escaped and the string looks like this: `"\"C:\\Users\\...\\file.txt\""`, which would need a more complicated regular expression than the one given in your answer to convert it to a usable path. If you use `scan(what="character")` instead of `readline()`, you can avoid this problem. –  Apr 07 '16 at 07:47
  • In RStudio you can run the following from the editor: `x <- readline() C:\Users\jd\Documents\folder\file.txt` where the commands are on a separate line. – Alex Jun 22 '16 at 01:29
53

If you want the least number of keystrokes to convert backslashes when pasting paths, use an RStudio snippet defined as follows:

snippet pp
    "`r gsub('"', "", gsub("\\\\", "/", readClipboard()))`"

Remember to preface the second line with a tab, not multiple spaces for the snippet to work.

Then type pp, TAB, ENTER and the text on your clipboard is pasted, backslashes replaced with forward slashes and surrounded by quotes.

Here is the steps I usually take to copy file paths to RStudio once the above snippet has been defined:

  1. Navigate to file path in explorer.
  2. If copying a file path then: Shift + Right click on file, then click Copy as path.
  3. If copying a folder path then: Alt + d, Ctrl + c.
  4. Change window to RStudio and focus in R script where you want to paste the path.
  5. pp, TAB, ENTER to paste into RStudio and convert backslashes to forward slashes.
Nova
  • 5,423
  • 2
  • 42
  • 62
Josh Gilfillan
  • 4,348
  • 2
  • 24
  • 26
  • 3
    2 years later, I write a snippet called `pp` and excitedly return to this question hoping to post it as an answer, only to see this :-( +1 – Josh Jul 26 '18 at 00:56
  • I don't understand your snippet, specifically the quotes `"` outside the ticks `\`` and the first `gsub()`. I used `"\`r gsub("\\\\", "/", readClipboard())\`"` – Josh Jul 26 '18 at 01:05
  • 2
    The gsub and quotes ensures that paths copied from the address bar in explorer (which don't have qutoes) and paths copied via "Copy as path" (which do have qutoes) are both pasted with quotes surrounding them. – Josh Gilfillan Jul 31 '18 at 03:50
18

I use Path Copy Copy, which is a plug-in to Windows that allows you to create custom copy commands when you right-click a file/folder in Windows. So my right-click menu has "Copy Full Path with Forward Slash" as an option, which copies the file/folder with forward slashes. I am guessing it saves me days every year from manually changing slashes to R's format.

Eden
  • 335
  • 2
  • 8
14

I like to use the RStudio add-in snippetsaddin which has the function 'Convert slash':

It will reverse all slashes either in the selected block(s) of code, or if there is no selection (or only whitespace is selected), it will reverse all slashes in the clipboard and paste it to the current cursor(s) position(s).

Addins are isntalled like a package. To install this one, do this:

devtools::install_github("sfr/RStudio-Addin-Snippets", type = "source")
userJT
  • 11,486
  • 20
  • 77
  • 88
Ben
  • 41,615
  • 18
  • 132
  • 227
12

If I understand correctly, you do want to get rid of the string editing. In order to be able to use gsub you would have to change all the \ to \\ manually first. So, why not just change \ to / in first place?

If you have the string in the clipboard you can use

  x=scan("clipboard",what="string")

This gives

  "C:\\Users\\jd\\Documents\\folder\\file.txt"

That is, it converts all \ to \\ automatically. I know - not very handy, but the only way I know to get around the editing.

cryo111
  • 4,444
  • 1
  • 15
  • 37
  • BTW: With "clipboard" I mean what you have copied with `CTRL+V`. – cryo111 Jul 12 '13 at 00:31
  • 1
    I don't get as expected when I have the folder name separed by spaces. For example `C:\Users\jd\Documents\my country\my name\file.txt`. – Jd Baba Jul 12 '13 at 00:36
  • Hmmm... good point. You could use a different separator `x=scan("clipboard",what="string",sep=";")`. I changed it to `;`. I believe semicolons are not allowed in file paths, right? Therefore, you should be on the safe side with choosing a semicolon as separator. – cryo111 Jul 12 '13 at 01:13
10

Here is a one step method of converting the address from the clipboard

x  <- gsub  ( "\\\\",  "/",  readClipboard ()  ) 
msrd0
  • 7,816
  • 9
  • 47
  • 82
craig_c
  • 111
  • 1
  • 2
10

autohotkey program:

^+v::
StringReplace, clipboard, clipboard, \,/,All
send %clipboard%

after control+c the file path, use control + shift + v to paste

hedgedandlevered
  • 2,314
  • 2
  • 25
  • 54
5

A solution without a snippet defintion is

writeClipboard(gsub("\\\\", "/", readClipboard()))
userJT
  • 11,486
  • 20
  • 77
  • 88
5

R has the inbuilt r"(C:\myfolder\)" command which converts backslashes in a string to double backslashes.

x <- r"(C:\myfolder\)"
print(x)

"C:\\myfolder\\"

Only problem is that it can't take a variable and can only take a typed string

pluke
  • 3,832
  • 5
  • 45
  • 68
0

I think the best way to get rid of the hassle is to find the file in Rstudio in the right panel. And then click "more" and click "Set as Working Directory". Then you will see in the console "setwd(...)". You can copy this to your code.

olk
  • 61
  • 1
  • 5
0

My method is the same as what @pluke stated.

This is how I set my working directory:

x <- r"(C:\myfolder\)" 
setwd(x)
Ramsey A.
  • 63
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33807586) – shs Feb 14 '23 at 16:32