2010-04-02 83 views
10

我的函數的參數的默認值包含「%」。這似乎是roxygen的一個問題,它會產生很多警告,而在嘗試構建latex文檔時,R CMD檢查會失敗。如何在roxygen literate programming中逃避%?

如何使此功能(及其文檔)工作?使用%%或\%代替%不會有幫助。

#' 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) 
} 
+0

是的,我都嘗試。我糾正了上面的代碼片段。 – 2010-04-02 10:52:22

+1

您是否直接通過電子郵件向開發人員發送電子郵件?據我所知,像大多數R開發人員,他們不掛在堆棧溢出 – hadley 2010-04-02 13:19:10

+0

不,我沒有,我認爲這是一個用戶錯誤。現在我加入了roxygen郵件列表並重新發布了這個問題。 – 2010-04-02 20:35:15

回答

6
#!/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 

這可能會引入更多的問題,例如,如果您使用%%作爲模運算符有示例代碼,但它對我來說很方便。

+5

或者使用roxygen2 – hadley 2011-12-26 14:05:10

+1

然後做什麼? – tim 2014-02-19 22:55:28

+5

@tim in roxygen 2您可以使用\% – JTextor 2015-10-06 10:11:23

5

代碼類似於問題中的代碼對我來說沒有問題,並生成正確的文檔。正如多個評論所說,原因必須是我正在使用roxygen2軟件包。所以,只用反斜槓使用roxygen2然後逃離「%」的文件中:

#' The percentage sign is written in the documentation like this: \%. 
+0

轉義百分號使用'roxygen2'我推測? – SymbolixAU 2016-06-22 11:49:46

+0

是的,你一定是對的,那一定是理由。我剛剛安裝了RStudio,並使用它隨附的任何內容。我會相應地改變我的答案。謝謝! – jciloa 2016-06-22 14:41:01