如何将未导出的函数添加到 R 包

How to add unexported functions to R Package

我正在开发一个 R Package,其中包含我在一个导出函数中使用的多个函数。

我的问题,可能是一个愚蠢的问题,我应该把所有这些 unexported 函数放在哪里?或者我怎样才能做到这一点?

我不想让用户从 R/ 目录访问此功能。

这是我使用未导出函数的虚构函数 add:

my_func <- function(x, y){
   result <- exp(add(x, y))
   return(result)
   }

这是我不想导出的虚构函数 add

add <- function(x, y) {
 result <- x + y
 return(result)
}

谢谢

编辑

如果我将所有功能都放在 /R 中,它们将对用户“可见”。例如,xgboost 包中的 generate.cv.folds 未导出,但您仍然可以使用 xgboost:::generate.cv.folds 访问它,我也想这样做。

R 不提供用于隐藏源代码的基础结构[1]。这样做违背了 R 开发人员的价值观和愿望。

所以,你想做的事用 R 是做不到的。

[1] http://r.789695.n4.nabble.com/how-to-hide-code-of-any-function-td4474822.html

Exporting/not 导出函数就像在函数文档中添加一行一样简单。我假设您正在使用 roxygen2 来记录您的包裹。

导出:

#' Add two numeric values
#'
#' This function returns the sum of two values.
#' @param x,y Numeric values.
#' @return Numeric.
#' @export
add1 <- function(x, y) {
 result <- x + y
 return(result)
}

未导出:

#' Add two numeric values.
#'
#' This function returns the sum of two values.
#' @param x,y Numeric values.
#' @return Numeric.
add2 <- function(x, y) {
 result <- x + y
 return(result)
}

请注意文档末尾缺少的 @export。创建文档时,NAMESPACE 文件将如下所示:

export(add1)

但它不会有 add2 的条目。