Roxygen: how to set a default parameter including backslash ('\\') to functions

两盒软妹~` 提交于 2019-12-04 22:26:47

I also ran into problems with too much escaped " and vanishing \t. I ended up changing the parse.formals function in roxygen's Rd2.R as follows:

  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))
    }
  }

Hope that helps and doesn't break anything else.

I came across this problem also, i.e. I had split="\+" and split="\|" in various functions.

The .Rd files actually also represent these as split="\+" and split="\|", but then whatever reads .Rd files in R CMD check turned them into split="+" and split="\|", and caused the error.

If the .Rd files had split="\\+" and split="\\|" it would then get read correctly.

My solution was to run this on the /man/ directory with the .Rd files, just before R CMD check:

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

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

Took a bit of trial and error, but it works!

Cheers! Nick

I had the same problem. I ended-up overriding default usage field as produced by roxygen from the source code with @usage. E.g. the R source file contains

#' @usage sourceall(dir = getwd(), pattern = ".*\\\\.R", ...)
sourceall <- function( dir=getwd(), pattern=".*\\.R", ... )
{
    blah blah
}

I experienced similar problem with 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))

In your case, with the newline character, it should also work:

lineCount <- function(text, sep = intToUtf8(10))

Well, frack me, the comment box escapes backslash characters! I refuse to add to the madness and add backslashes to escape the blackslashes in the perl code which escape the backslashes in the .Rd files which escape the backlashes in R.

Just double all backslashes.

Or hmm code tags:

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

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

Yeah that's good.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!