在包中创建二元运算符函数

时间:2012-02-10 09:17:20

标签: r

我正在尝试将二进制运算符函数添加到我的包中,但它没有加载包。例如,我定义了这个函数并将其保存为名为'wo.R'的文件

`%wo%` <- function(x, y) {
    x[!x %in% y]
}

并创建文档文件'wo.Rd'

\name{\%wo\%}
\alias{\%wo\%}
\title{Without}
\description{Elements in one vector without matching elements in a second vector.}
\usage{x \%wo\% y}
\arguments{
  \item{x}{A vector.}
  \item{y}{A vector.}
}
\value{A vector.}
\author{me, based on example in the \code{\link{match}} function documentation.}
\examples{
(1:10) \%wo\% c(3,7,12)
}

当我运行R CMD check myPackage时,在检查文档示例时会出现此错误:Error: could not find function "%wo%" Execution halted。我可以删除该示例,并成功安装我的包,但%wo%函数未加载我的包。我可以在R会话中获取'wo.R'文件并且它可以工作。我也可以将函数定义为wo <- function(x, y) x[!x %in% y],这似乎工作正常。我探索了其他软件包的源代码,比如'运营商',我的源代码和文档文件似乎与它们一致,但我显然忽略了一些东西。

1 个答案:

答案 0 :(得分:13)

您需要在NAMESPACE中导出该功能。

在文档文件中添加export语句:

export("%wo%")