简体   繁体   中英

Efficiently convert backslash to forward slash in R

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.

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"

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.

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.

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

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.

这是从剪贴板转换地址的一步方法

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

autohotkey program:

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

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

没有片段定义的解决方案是

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM