macos 一个错误 ['\+' is an unrecognized escape in the string starts at "\+" while 创建 R 包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10605485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:10:07  来源:igfitidea点击:

An error ['\+' is an unrecognized escape in character string starting "\+" while creating a R package

macosrpackagex11

提问by Alex

I tried to create a package using some functions and scripts I created (using X11 on a Mac). While R CMD check was doing its work, it encountered a problem as follows:

我尝试使用我创建的一些函数和脚本创建一个包(在 Mac 上使用 X11)。R CMD check 在做它的工作时,遇到了如下问题:

temp = trim(unlist(strsplit(lp.add(ranefterms[[i]]), 
+                     "\+")))
Error: '\+' is an unrecognized escape in character string starting "\+"

The oddest thing, however, is that my function actually does NOT have "\ +". Instead, it has "\ \ +" (see below). So I don't know why "\ \ +" is recognized as "\ +".

然而,最奇怪的是我的函数实际上没有“\ +”。相反,它有“\ \ +”(见下文)。所以我不知道为什么“\ \ +”被识别为“\ +”。

for(i in 1:n)
   temp = trim(unlist(strsplit(lp.add(ranefterms[[i]]), '\+')))

To dig a little further, I looked at the packageName-Ex.R file in the Rcheck folder. As it turned out, all the "\ \"s have been changed to "\" in the checking process (e.g., the double slashes I need for functions such as strsplit() and grepl())

为了进一步挖掘,我查看了 Rcheck 文件夹中的 packageName-Ex.R 文件。原来,在检查过程中,所有的“\”都变成了“\”(比如strsplit()和grepl()等函数我需要的双斜线)

I wonder what may have been the cause of it. Sorry that I can't come up with a reproducible example...

我想知道可能是什么原因造成的。对不起,我不能想出一个可重现的例子......

采纳答案by Josh O'Brien

The offending code comes from the Examples section of one of your help files (which is why it ends up in packageName-Ex.R). To fix it, just escape each of the backslashes in the Examples sections of your *.Rddocumentation files with a second backslash. (So, type \\to get \in the processed help file, and type \\\\to get \\.)

有问题的代码来自您的一个帮助文件的示例部分(这就是它以 结尾的原因packageName-Ex.R)。要修复它,只需*.Rd使用第二个反斜杠转义文档文件示例部分中的每个反斜杠。(因此,键入\\以获取\已处理的帮助文件,然后键入\\\\以获取\\。)

Unless it's escaped, \is interpreted as a special character that identifies sectioning and mark-up macros (i.e. commands like \author, \description, \bold, and \ldots). Quoting from Duncan Murdoch's Parsing Rd files(the official manual for this topic):

除非它是逃过一劫,\被解释为特殊字符识别切片和标记宏(即命令状\author\description\bold,和\ldots)。引用 Duncan Murdoch 的Parsing Rd 文件(本主题的官方手册):

The backslash \ is used as an escape character: \, \%, { and } remove the special meaning of the second character.

反斜杠\ 用作转义字符:\、\%、{ 和} 删除第二个字符的特殊含义。

As an example of what this looks like in practice, here is part of $R_SOURCE_HOME/src/library/base/man/grep.Rd, which is processed to create the help file you see when you type ?grepor ?gsub.

作为实际情况的示例,这里是 的一部分$R_SOURCE_HOME/src/library/base/man/grep.Rd,它被处理以创建您在键入?grep或时看到的帮助文件?gsub

## capitalizing
txt <- "a test of capitalizing"
gsub("(\\w)(\\w*)", "\\U\\1\\L\\2", txt, perl=TRUE)
gsub("\\b(\\w)",    "\\U\\1",       txt, perl=TRUE)

In the processed help file, it looks like this:

在处理后的帮助文件中,它是这样的:

 ## capitalizing
 txt <- "a test of capitalizing"
 gsub("(\w)(\w*)", "\U\1\L\2", txt, perl=TRUE)
 gsub("\b(\w)",    "\U\1",       txt, perl=TRUE)