简体   繁体   English

在 R 中的字符串或路径中转义反斜杠 (\\)

[英]Escaping backslash (\) in string or paths in R

Windows copies path with backslash \\ , which R does not accept. Windows 使用反斜杠\\复制路径,R 不接受。 So, I wanted to write a function which would convert \\ to / .所以,我想编写一个将\\转换为/的函数。 For example:例如:

chartr0 <- function(foo) chartr('\','\\/',foo)

Then use chartr0 as...然后使用chartr0作为...

source(chartr0('E:\RStuff\test.r'))

But chartr0 is not working.但是chartr0不起作用。 I guess, I am unable to escape / .我想,我无法逃脱/ I guess escaping / may be important in many other occasions.我想在许多其他场合逃避/可能很重要。

Also, is it possible to avoid the use chartr0 every time, but convert all path automatically by creating an environment in R which calls chartr0 or use some kind of temporary use like using options此外,是否可以避免每次都使用chartr0 ,而是通过在R中创建一个调用chartr0的环境或使用某种临时使用(如使用options自动转换所有路径

From R 4.0.0 you can use r"(...)" to write a path as raw string constant , which avoids the need for escaping:R 4.0.0 开始,您可以使用r"(...)"将路径写为原始字符串常量,从而避免了转义:

r"(E:\RStuff\test.r)"
# [1] "E:\\RStuff\\test.r"

There is a new syntax for specifying raw character constants similar to the one used in C++: r"(...)" with ... any character sequence not containing the sequence )" . This makes it easier to write strings that contain backslashes or both single and double quotes. For more details see ?Quotes .有一种新的语法用于指定类似于 C++ 中使用的原始字符常量的语法: r"(...)" with ...任何不包含序列)"字符序列。这使得编写包含反斜杠的字符串更容易或单引号和双引号。有关更多详细信息,请参阅?Quotes

Your fundamental problem is that R will signal an error condition as soon as it sees a single back-slash before any character other than a few lower-case letters, backslashes themselves, quotes or some conventions for entering octal, hex or Unicode sequences.您的基本问题是,R 会在除几个小写字母、反斜线本身、引号或用于输入八进制、十六进制或 Unicode 序列的一些约定之外的任何字符之前看到单个反斜杠时,立即发出错误信号。 That is because the interpreter sees the back-slash as a message to "escape" the usual translation of characters and do something else.那是因为解释器将反斜杠视为“逃避”通常的字符翻译并做其他事情的消息。 If you want a single back-slash in your character element you need to type 2 backslashes.如果您想在字符元素中使用单个反斜杠,则需要键入 2 个反斜杠。 That will create one backslash:这将创建一个反斜杠:

nchar("\\")
#[1] 1

The "Character vectors" section of _Intro_to_R_ says: _Intro_to_R_ 的“字符向量”部分说:

"Character strings are entered using either matching double (") or single (') quotes, but are printed using double quotes (or sometimes without quotes). "字符字符串使用匹配的双 (") 或单 (') 引号输入,但使用双引号(或有时不带引号)打印。 They use C-style escape sequences, using \\ as the escape character, so \\ is entered and printed as \\, and inside double quotes " is entered as \\".他们使用 C 风格的转义序列,使用 \\ 作为转义字符,所以 \\ 被输入并打印为 \\,并且在双引号内“被输入为 \\”。 Other useful escape sequences are \\n, newline, \\t, tab and \\b, backspace—see ?Quotes for a full list."其他有用的转义序列是 \\n、换行符、\\t、制表符和 \\b、退格符——请参阅 ?Quotes 以获取完整列表。”

 ?Quotes
chartr0 <- function(foo) chartr('\\','/',foo)
chartr0('E:\\RStuff\\test.r')

You cannot write E:\\Rxxxx, because R believes R is escaped.你不能写 E:\\Rxxxx,因为 R 认为 R 被转义了。

The problem is that every single forward slash and backslash in your code is escaped incorrectly, resulting in either an invalid string or the wrong string being used.问题是代码中的每个正斜杠和反斜杠都被错误地转义,导致使用无效字符串或错误字符串。 You need to read up on which characters need to be escaped and how.您需要阅读哪些字符需要转义以及如何转义。 Take a look at the list of escape sequences in the link below.查看以下链接中的转义序列列表。 Anything not listed there (such as the forward slash) is treated literally and does not require any escaping.此处未列出的任何内容(例如正斜杠)均按字面处理,不需要任何转义。

http://cran.r-project.org/doc/manuals/R-lang.html#Literal-constants http://cran.r-project.org/doc/manuals/R-lang.html#Literal-constants

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

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