2010-04-02 20 views
10

Giá trị mặc định của tham số của hàm của tôi chứa "%". Điều này có vẻ là một vấn đề đối với roxygen, nó tạo ra rất nhiều cảnh báo và R CMD kiểm tra thất bại khi cố gắng xây dựng tài liệu latex.Làm thế nào để thoát khỏi% trong lập trình biết chữ roxygen?

Làm cách nào để làm cho chức năng này (và tài liệu của nó hoạt động)? Sử dụng %% hoặc \% thay vì% không giúp ích gì.

#' 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

Có tôi đã thử cả hai. Tôi đã sửa đoạn mã ở trên. –

+1

Bạn đã gửi email trực tiếp cho nhà phát triển chưa? Theo như tôi biết, giống như hầu hết các nhà phát triển R, họ không đi chơi trên tràn ngăn xếp – hadley

+0

Không, tôi đã không nghĩ rằng đây là lỗi người dùng. Bây giờ tôi đã gia nhập danh sách gửi thư của roxygen và đăng lại câu hỏi. –

Trả lời

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

Đây là giải pháp thanh nha tôi. Save the script perl như, ví dụ, escape_percents.pl, sau đó trình tự sẽ là như thế này:

R CMD roxygen my.package 
perl escape_percents.pl my.package.roxygen/man/*.Rd 
R CMD install my.package.roxygen 

này có thể giới thiệu nhiều vấn đề, ví dụ nếu bạn có mã ví dụ sử dụng %% như các nhà điều hành mô đun, nhưng nó có ích cho tôi.

+5

Hoặc sử dụng roxygen2 – hadley

+1

và sau đó làm gì? – tim

+5

@tim trong roxygen 2 bạn có thể thoát dấu phần trăm bằng cách sử dụng \% – JTextor

5

Mã tương tự với mã trong câu hỏi phù hợp với tôi mà không gặp sự cố và tạo tài liệu chính xác. Như nhiều ý kiến ​​nói, lý do phải là tôi đang sử dụng gói roxygen2. Vì vậy, chỉ cần sử dụng roxygen2 và sau đó thoát "%" trong tài liệu bằng cách sử dụng dấu gạch chéo ngược:

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

Sử dụng 'roxygen2' tôi đoán? – SymbolixAU

+0

Có, bạn phải đúng, đó phải là lý do. Tôi vừa cài đặt RStudio và sử dụng bất cứ thứ gì đi kèm với nó. Tôi sẽ thay đổi câu trả lời của tôi cho phù hợp. Cảm ơn! – jciloa

Các vấn đề liên quan