简体   繁体   English

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

[英]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:我正在寻找一种在 R 中将反斜杠转换为正斜杠的有效方法。有时我在 Windows 中复制目录的链接,我得到如下信息:

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

How can I quickly change this to C:/Users/jd/Documents/folder/file.txt ?如何快速将其更改为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". "\u 在以 ""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.我知道 R 中的 TAB 函数有助于快速找到位置,但只是想知道是否还有其他解决方法。 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:在 R 中,你必须用\\转义\所以,你的路径应该是:

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)然后,在提示符下,粘贴您未修改的路径(CTRL+V 然后 ENTER)

Finally, to change \\ to / everywhere, you could use gsub , once again by escaping the \ , but twice, as follows:最后,要将\\更改为/任何地方,您可以使用gsub ,再次转义\ ,但两次,如下所示:

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:如果您希望在粘贴路径时使用最少的击键次数来转换反斜杠,请使用如下定义的 RStudio 代码段

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

Remember to preface the second line with a tab , not multiple spaces for the snippet to work.请记住在第二行前面加上一个tab ,而不是多个空格以使代码段起作用。

Then type pp , TAB , ENTER and the text on your clipboard is pasted, backslashes replaced with forward slashes and surrounded by quotes.然后键入ppTABENTER并粘贴剪贴板上的文本,将反斜杠替换为正斜杠并用引号括起来。

Here is the steps I usually take to copy file paths to RStudio once the above snippet has been defined:一旦定义了上述代码段,我通常会采取以下步骤将文件路径复制到 RStudio:

  1. Navigate to file path in explorer.导航到资源管理器中的文件路径。
  2. If copying a file path then: Shift + Right click on file, then click Copy as path .如果复制文件路径,则: Shift +右键单击​​文件,然后单击Copy as path
  3. If copying a folder path then: Alt + d , Ctrl + c .如果复制文件夹路径,则: Alt + dCtrl + c
  4. Change window to RStudio and focus in R script where you want to paste the path.将窗口更改为 RStudio,然后将焦点放在要粘贴路径的 R 脚本中。
  5. pp , TAB , ENTER to paste into RStudio and convert backslashes to forward slashes. ppTABENTER粘贴到 RStudio 并将反斜杠转换为正斜杠。

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.我使用Path Copy Copy ,它是 Windows 的一个插件,允许您在 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.我猜每年手动将斜杠更改为 R 的格式可以节省我的时间

I like to use the RStudio add-in snippetsaddin which has the function 'Convert slash' :我喜欢使用具有 “转换斜线”功能的 RStudio 插件片段插件:

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).它将反转所选代码块中的所有斜杠,或者如果没有选择(或仅选择空格),它将反转剪贴板中的所有斜杠并将其粘贴到当前光标位置( 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.为了能够使用gsub ,您必须先手动将所有\更改为\\ 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在 control+c 文件路径后,使用 control + shift + v 粘贴

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

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

R has the inbuilt r"(C:\myfolder\)" command which converts backslashes in a string to double backslashes. R 具有内置的r"(C:\myfolder\)"命令,可将字符串中的反斜杠转换为双反斜杠。

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

"C:\\myfolder\\" "C:\\我的文件夹\\"

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.我认为摆脱麻烦的最好方法是在 Rstudio 的右侧面板中找到该文件。 And then click "more" and click "Set as Working Directory".然后单击“更多”并单击“设置为工作目录”。 Then you will see in the console "setwd(...)".然后您将在控制台中看到“setwd(...)”。 You can copy this to your code.您可以将其复制到您的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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