Efficiently convert backslash to forward slash in R

REscapingFilepath

R Problem Overview


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.

R Solutions


Solution 1 - R

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"

Solution 2 - R

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.

Solution 3 - R

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.

Solution 4 - R

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")

Solution 5 - R

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.

Solution 6 - R

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

x  <- gsub  ( "\\\\",  "/",  readClipboard ()  ) 

Solution 7 - R

autohotkey program:

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

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

Solution 8 - R

A solution without a snippet defintion is

writeClipboard(gsub("\\\\", "/", readClipboard()))

Solution 9 - R

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.

Solution 10 - R

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJd BabaView Question on Stackoverflow
Solution 1 - RArunView Answer on Stackoverflow
Solution 2 - RJosh GilfillanView Answer on Stackoverflow
Solution 3 - REdenView Answer on Stackoverflow
Solution 4 - RBenView Answer on Stackoverflow
Solution 5 - Rcryo111View Answer on Stackoverflow
Solution 6 - Rcraig_cView Answer on Stackoverflow
Solution 7 - RhedgedandleveredView Answer on Stackoverflow
Solution 8 - RuserJTView Answer on Stackoverflow
Solution 9 - RolkView Answer on Stackoverflow
Solution 10 - RplukeView Answer on Stackoverflow