我正在开发一个带有RStudio的自定义R包,用roxygen2记录.
请考虑这个功能:
#' Get "test"
#'
#' @return String
#' @export
#'
#' @examples getTest()
getTest <- function() {
return("test")
}
Run Code Online (Sandbox Code Playgroud)
如果我R CMD check使用上面编写的函数文档运行,一切都很好,check成功传递.
现在,如果我删除@export(因为我不希望从包外部看到此函数),我收到以下错误:
* checking examples ... ERROR
Running examples in 'MyPackageName-Ex.R' failed
The error most likely occurred in:
> ### Name: getTest
> ### Title: Get "test"
> ### Aliases: getTest
>
> ### ** Examples
>
> getTest()
Error: could not find function "getTest"
Execution halted
Run Code Online (Sandbox Code Playgroud)
看起来函数的测试@examples是从包外面运行的!?
如何测试非导出函数的示例?
我恭敬地不同意@Roland 的评论,因为有一个与文档接近的示例有助于了解我在 6 个月后到底在想什么。让它自动检查check()意味着它有机会与代码保持同步。我在 R 文档中没有看到任何禁止或告诫记录非导出函数的内容。
幸运的是,您可以使用三分号:::运算符调用未导出的函数。例子:
##' Drop specified dimension from an array
##'
##' Like drop(x) but only dropping specified dimensions.
##' There is no testing that the specified dimensions are actually singletons.
##' @param x array of at least d dimensions
##' @param d dimension(s) to drop
##' @return array x
##' @examples
##' x = array(1:4, dim=c(1, 2, 1, 2))
##' dx = MAST:::Drop(x, 1)
##' stopifnot(all(dim(dx)==c(2,1,2)))
##'
Drop <- function(x, d){
dim(x) <- dim(x)[-d]
x
}
Run Code Online (Sandbox Code Playgroud)