如何在roxygen文学编程逃避%?
-
23-09-2019 - |
题
我的函数的参数的缺省值包含“%”。这似乎是roxygen一个问题,它会产生大量的警告,并试图建立胶乳文档当R CMD检查失败。
我怎样才能让这个功能(和它的文档)的工作?使用%%或\%而不是%没有帮助。
#' Test escape \% from in-source documentation (roxygen).
#'
#' What happens when parameters contain special latex characters?
#'
#' @param x unsuspicious parameter
#' @param format sprintf format string (default "\%5.0f")
#'
#' @return formatted string
#' @export
#' @author Karsten Weinert
testroxy <- function(x, format = "%5.0f") {
sprintf(format,x)
}
解决方案
#!/usr/bin/perl
use strict;
use File::Temp qw/tempfile/;
use File::Copy;
my $usage = <<EOD
$0 file1.Rd [file2.Rd ...]
When using roxygen to generate documentation for an R pacakge, if a default
argument has a percent sign in it then roxygen will copy it directly into
the .Rd file. Since .Rd is basically latex, this will be interpreted as a
comment and case the file to be parsed incorrectly.
For percent signs elsewhere in your documentation, for example in the
description of one of the parameters, you should use "\%" so parse_Rd
interprets it correctly.
But you cannot do this in the default arguments because they have to be
valid R code, too.
Since the .Rd files are automatically generated they should not have
any latex comments in them anyway.
This script escapes every unescaped % within the file.
The .Rd files are modified in place, since it would be easy to
generate them again with R CMD roxygen.
EOD
;
my $n_tot = 0;
my $n_file = @ARGV;
my $n_esc_file = 0;
foreach my $fn (@ARGV) {
print STDERR ' ' x 100, "\rWorking on $fn\t";
open my $fh, $fn or die "Couldn't open $fn: $!";
my ($tmp_fh, $tmp_fn) = tempfile();
my $n;
while(<$fh>) {
$n += s/(?<!\\)%/\\%/g; # if % is not preceded with backslash then replace it with '\%'
print $tmp_fh $_;
}
$n_tot += $n;
$n_esc_file ++ if $n;
print "Escaped $n '%'\n" if $n;
close $tmp_fh;
move($tmp_fn => $fn);
}
print "\n";
print "$n_file files parsed\n";
print "$n_esc_file contained at least one unescaped %\n";
print "$n_tot total % were escaped\n";
这是我的不雅溶液。保存perl脚本,例如,escape_percents.pl,则该序列将是这样的:
R CMD roxygen my.package
perl escape_percents.pl my.package.roxygen/man/*.Rd
R CMD install my.package.roxygen
这可能引入更多的问题,例如如果在使用作为%%取模运算符代码示例,但它已经得心应手我。
其他提示
代码一个类似的问题,工作对我来说没有问题,并产生正确的文档。由于多个评论说,之所以必须是我使用的roxygen2包。所以只是使用roxygen2,然后使用逃脱“%”的文档中反斜杠:
#' The percentage sign is written in the documentation like this: \%.
不隶属于 StackOverflow