在 R 中有效地将反斜杠转换为正斜杠

我正在寻找一种有效的方法,将 R 中的反斜杠转换为正斜杠。有时候,我复制 Windows 中目录的链接,得到的结果如下:

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

我怎样才能快速地把它改成 C:/Users/jd/Documents/folder/file.txt?我甚至不能把上面的表达理解为字符。它会抛出一个错误

“在字符串开头没有十六进制数字的 u”“ C: u”。

我知道 R 中的 TAB 函数有助于快速找到位置,但我只是想知道是否还有其他工作可做。我也可以把工作目录改成文件夹的位置。我只是随便玩玩,试着把反斜杠转换成正斜杠,但不是直截了当的,所以只是出于好奇才问这个问题。

71468 次浏览

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 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.

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

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

autohotkey program:

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

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

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.

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.

A solution without a snippet defintion is

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

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

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.

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