错误['\ +'是无法识别的以'\ +'开头的字符串.在创建R包时 [英] An error ['\+' is an unrecognized escape in character string starting "\+" while creating a R package
问题描述
我尝试使用一些我创建的功能和脚本(在Mac上使用X11)创建一个软件包. R CMD检查在执行工作时遇到以下问题:
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:
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]]), '\\+')))
为了进一步探讨,我查看了Rcheck文件夹中的packageName-Ex.R文件.事实证明,在检查过程中所有的"\ \"都已更改为"\"(例如,strsplit()和grepl()之类的函数需要双斜杠
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())
我想知道是什么原因造成的.抱歉,我无法提供可重复的示例...
I wonder what may have been the cause of it. Sorry that I can't come up with a reproducible example...
推荐答案
有问题的代码来自您一个帮助文件的示例"部分(这就是为什么它以packageName-Ex.R
结尾)的原因.要解决此问题,只需在*.Rd
文档文件的示例"部分中的每个反斜杠转义第二个反斜杠即可. (因此,键入\\
以获得处理后的帮助文件中的\
,然后键入\\\\
以获得\\
.)
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 *.Rd
documentation files with a second backslash. (So, type \\
to get \
in the processed help file, and type \\\\
to get \\
.)
除非将其转义,否则\
会被解释为一个特殊字符,用于标识分段和标记宏(即\author
,\description
,\bold
和\ldots
之类的命令).引用邓肯·默多克(Duncan Murdoch)的 解析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):
反斜杠\用作转义字符:\,\%,{和} 删除第二个字符的特殊含义.
The backslash \ is used as an escape character: \, \%, { and } remove the special meaning of the second character.
作为实际情况的一个示例,这是$R_SOURCE_HOME/src/library/base/man/grep.Rd
的一部分,该内容被处理以创建您在键入?grep
或?gsub
时看到的帮助文件.
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 ?grep
or ?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)
这篇关于错误['\ +'是无法识别的以'\ +'开头的字符串.在创建R包时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!