22

我使用 Roxygen 来生成我正在开发的包的 Rd 文件,但是我在默认参数设置为 ' 的函数方面遇到了一些问题\n',例如:

  lineCount <- function(text, sep='\n') {
       ...
   }

其目的是计算'\n'字符串中的换行 ( ) 字符。问题是 R CMD check 给出警告:

Codoc mismatches from documentation object 'lineCount':
lineCount
  Code: function(text, sep = "\n")
  Docs: function(text, sep = " ")
  Mismatches in argument default values:
    Name: 'sep' Code: "\n" Docs: " "

在我看来,这个问题是由写入 Rd 文件引起的(通过写入标准 LaTeX 文件cat()总是需要出于某种目的使用双转义字符,例如:\\newline - 正如我所经历的那样)。如果我在分隔符上加上一个额外的反斜杠,例如:

  lineCount <- function(text, sep='\\n') {
       ...
   }

问题仍然存在,就像它看起来像的代码一样'\\n',但在文档(Rd 文件)中它看起来像'\n'.

我的问题有简单的解决方案吗?可能是 Roxygen 中的一个额外标签,它可以定义如何将函数的参数写入 Rd 文件?抱歉,如果问了一个太明显的问题,但是在谷歌搜索了一段时间后我迷路了。


历史: http: //permalink.gmane.org/gmane.comp.lang.r.roxygen/24


更新:使用roxygen2

4

5 回答 5

3

我也遇到了太多转义“和消失\t的问题。我最终改变了parse.formalsroxygen的Rd2.R中的函数,如下所示:

  parse.formals <- function(partitum) {
    formals <- partitum$formals
    if (!is.null(formals)) {
      formals <- lapply(formals, trim)
      formals <- lapply(formals, paste, collapse=" ")
      name.defaults <- zip.c(names(formals), formals)
      args <-
        do.call(paste, c(Map(function(name.default) {
          name <- car(name.default)
          default <- cadr(name.default)
          if (! is.character (default)) {  # too much escaped. 
                                           # Not sure when escaping is needed. 
                                           # param = c ("x", "y", "z") works now
            default <- gsubfn("\"(.*)\"",
                              function(x)
                              sprintf("\"%s\"", gsub("\"", "\\\\\"", x)),
                              as.character(default))
          }
          default <- gsub ("\t", "\\\\t", default) # the tabs and newlines are already
          default <- gsub ("\n", "\\\\n", default) # tab and newline here.
          if (is.null.string(default))
            name
          else
            sprintf('%s=%s', name, default)
        },
                             name.defaults),
                         sep=', '))

      append.Rd(usageTag(parse.function.name(partitum), args))
    }
  }

希望这会有所帮助,并且不会破坏其他任何东西。

于 2011-07-15T09:14:54.377 回答
0

我也遇到了这个问题,即我有 split="\+" 和 split="\|" 在各种功能中。

.Rd 文件实际上也将它们表示为 split="\+" 和 split="\|",但是在 R CMD 检查中读取 .Rd 文件的任何内容都将它们转换为 split="+" 和 split="\|",并导致错误。

如果 .Rd 文件有 split="\\+" 和 split="\\|" 然后它会被正确读取。

我的解决方案是在带有 .Rd 文件的 /man/ 目录上运行它,就在 R CMD 检查之前:

perl -e 's/("\\\|")/"\\\\\|"/g;' -pi $(查找 BioGeoBEARS/man -type f)

perl -e 's/("\\+")/"\\\\+"/g;' -pi $(查找 BioGeoBEARS/man -type f)

进行了一些试验和错误,但它有效!

干杯! 缺口

于 2013-04-13T00:27:23.877 回答
0

我有同样的问题。我最终覆盖了 roxygen 从源代码中使用@usage 生成的默认使用字段。例如,R 源文件包含

#' @usage sourceall(dir = getwd(), pattern = ".*\\\\.R", ...)
sourceall <- function( dir=getwd(), pattern=".*\\.R", ... )
{
    blah blah
}
于 2013-06-26T14:04:41.040 回答
0

我在使用 roxygen2 v6.0.1 时遇到了类似的问题。

# this caused errors in R CMD check (non-ASCII chars)
f <- function(x, y, chr = "\u279B")

# this was ok
f <- function(x, y, chr = intToUtf8(0x279B))

在您的情况下,使用换行符,它也应该可以工作:

lineCount <- function(text, sep = intToUtf8(10))
于 2017-02-24T20:23:38.580 回答
-1

好吧,让我崩溃,评论框转义了反斜杠字符!我拒绝增加疯狂并添加反斜杠来转义 perl 代码中的黑斜杠,从而转义 .Rd 文件中的反斜杠,从而转义 R 中的反斜杠。

只需将所有反斜杠加倍。

或者嗯代码标签:

perl -e 's/("\\\\\|")/"\\\\\\\\\|"/g;' -pi $(find BioGeoBEARS/man -type f)

perl -e 's/("\\\\\+")/"\\\\\\\\\+"/g;' -pi $(find BioGeoBEARS/man -type f)

是的,这很好。

于 2013-04-13T00:30:49.357 回答