2012-12-11 29 views
4

舉一個簡單的,具體的例子:Roxygen如何處理中綴二元運算符(例如%in%)?

#' Inverse Value Matching 
#' 
#' Complement of \code{%in%}. Returns the elements of \code{x} that are 
#' not in \code{y}. 
#' @usage x %nin% y 
#' @param x a vector 
#' @param y a vector 
#' @export 
"%nin%" <- function(x, y) { 
    return(!(x %in% y)) 
} 

然而,當我試圖構建一個軟件包的功能似乎被忽略,並且不產生文檔。

http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Documenting-functions似乎有關於二元中綴函數的單行blurb,但我很難解析它,以及它對Roxygen文檔意味着什麼。

回答

6

您需要在使用部分跳過% s。另外,我覺得你可能需要指定一個rdname

#' Inverse Value Matching 
#' 
#' Complement of \code{%in%}. Returns the elements of \code{x} that are 
#' not in \code{y}. 
#' @usage x \%nin\% y 
#' @param x a vector 
#' @param y a vector 
#' @export 
#' @rdname nin 
"%nin%" <- function(x, y) { 
    return(!(x %in% y)) 
} 

這是一個功能我有一個個人的包。我不認爲我曾經使用過這個功能,但roxygenize確實創建了一個幫助文件,並且包通過了R CMD check

#' percent in 
#' 
#' calculate the percentage of elements of \code{table} that are in \code{x} 
#' 
#' @param x vector or NULL: the values to be matched 
#' @param table vector or NULL: the values to be matched against 
#' @return percentage of elements of \code{x} that are in \code{table} 
#' @author gsee 
#' @usage x \%pctin\% table 
#' @examples 
#' letters[1:10] %pctin% letters[1:3] # 30% of the second arg ar in the first 
#' @export 
#' @rdname PctIn 
"%pctin%" <- function(x, table) length(x[x %in% table])/length(x) 
+0

這就是訣竅。謝謝! –

+0

您確定需要手動使用嗎? – hadley

+0

@hadley,沒有它我得到一個注意'prepare_Rd:PctIn.Rd:4-6:刪除空白部分\用法。除了NOTE之外,它似乎工作正常。 – GSee

1

我已經很難與roxygen和綴運營商(的roxygenroxygen2說)。以下是我的設置(R 2.15.1,roxygen 0.1-3)。

首先解決方法:編輯Rd文件中的每個管道符的(應該是grapes ... grapes.Rd)和每%\alias\usage\name份baskslash逃脫。

第二種解決方案:在中綴操作符的文檔中指定標籤@name和@usage並轉義%。這裏有一個例子:

##' Concatenates two strings 
##' 
##' @name \%+\% 
##' @usage \%+\%(x, y) 
##' @title Concatenation operator. 
##' @param a String. 
##' @param b String. 
##' @return Same as paste0(a, b). 
"%+%" <- function(a, b) paste(a, b, sep = "")