Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I use Roxygen to generate Rd files of my packages under development, but I have some problems with functions with default parameter set to '\n', e.g.:

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

Which purpose is to count new line ('\n') characters in a string. The problem is that R CMD check gives a warning about:

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: " " 

The problem seems to me that caused by writing to the Rd file (writing to standard LaTeX files via cat() always requires to double escape characters for some purpose, e.g.: \\newline - as I experienced). If I put an extra backslash to the separator, like:

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

The problem still presists, as in the code it looks like '\\n', but in the docs (Rd files) it looks '\n'.

Is there an easy solution for my problem? May be an extra tag in Roxygen which could define how to write the function's params to the Rd file? Sorry if asked too obvious a question, but I am lost after Google-ing for a while.


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


UPDATE: use roxygen2!

like image 477
daroczig Avatar asked Jan 06 '11 14:01

daroczig


1 Answers

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.

like image 80
cbeleites unhappy with SX Avatar answered Nov 03 '22 21:11

cbeleites unhappy with SX