Roxygen:如何设置默认参数,包括反斜杠('\')到函数

dar*_*zig 22 r roxygen

我使用Roxygen来生成正在开发的包的Rd文件,但是我的函数默认参数设置为' \n',例如:

  lineCount <- function(text, sep='\n') {
       ...
   }
Run Code Online (Sandbox Code Playgroud)

用于计算'\n'字符串中的新line()字符的目的是什么.问题是R CMD检查发出警告:

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: " "
Run Code Online (Sandbox Code Playgroud)

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

  lineCount <- function(text, sep='\\n') {
       ...
   }
Run Code Online (Sandbox Code Playgroud)

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

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


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


更新:使用roxygen2!

cbe*_*ica 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))
    }
  }
Run Code Online (Sandbox Code Playgroud)

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