19

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]]), '\\+')))

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

Alex
  • 4,030
  • 8
  • 40
  • 62

1 Answers1

19

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

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.

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)
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • @Josh-obrien Thanks. The help file was indeed the problem. Do you know why this is a problem? I created a package in the past and did not encounter a similar problem - I didn't change any of "\\"s to "\\\\"s. – Alex May 15 '12 at 17:37
  • @X.He -- This isn't something new, so I'll bet your earlier package didn't have any `\\ ` in your examples, **or** that any examples were inside of a `\dontrun{}` block, **or** that you didn't run R CMD check. Remember, this is not a problem having to do with the code in any of your packaged functions. Instead, it's to do with the way `*.Rd` files are processed. – Josh O'Brien May 15 '12 at 17:45