2010-04-02 3 views
10

내 함수의 매개 변수 기본값은 "%"를 포함합니다. 이것은 roxygen에서 문제가되는 것처럼 보입니다. 라텍스 문서를 만들 때 많은 경고와 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

예 둘 다 시도했습니다. 위 코드 조각을 수정했습니다. –

+1

개발자에게 이메일을 직접 보냈습니까? 내가 아는 한, 대부분의 R 개발자와 마찬가지로, 스택 오버플로에서 어울리지 않는다. – hadley

+0

아니, 나는 이것이 사용자 오류라고 생각했다. 이제 roxygen 메일 링리스트에 가입하여 질문을 다시 게시했습니다. –

답변

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

이 내 우아 솔루션입니다. 당신은 나머지 연산자로 %% 사용 예제 코드가있는 경우이 예를 들어, 더 많은 문제를 초래할 수있다

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

하지만, 예를 들어, 같은 펄 스크립트를 저장, escape_percents.pl, 다음 순서는 다음과 같이 될 것이다 그것은 나를 위해 편리했습니다.

+5

또는 roxygen2 – hadley

+1

을 사용하고 무엇을합니까? – tim

+5

roxygen 2에서 @tim \ % – JTextor

5

문제의 코드와 비슷한 코드가 문제없이 작동하고 올바른 설명서가 생성됩니다. 여러 의견에 따르면, 이유는 내가 roxygen2 패키지를 사용하고 있어야한다는 것입니다. roxygen2를 사용하고 백 슬래시를 사용하는 문서에서 "%"를 이스케이프 처리하십시오.

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

을 사용하여 백분율 기호를 이스케이프 처리 할 수 ​​있습니다. 'roxygen2'를 사용합니까? – SymbolixAU

+0

예, 당신이 옳을 것입니다. 그 이유가 틀림 없습니다. 방금 RStudio를 설치했고 함께 제공된 모든 것을 사용했습니다. 그에 따라 내 대답을 바꿀 것입니다. 감사! – jciloa

관련 문제