NAMESPACE 中的 S3methods 未导出

Mer*_*yde 6 namespaces r devtools

我正在R使用 devtools::document()开发一个包来创建 NAMESPACE 文件。其中一些函数是 S3methods,用于摘要、预测、绘图、打印,其中泛型在base或 中stats。我正在按照 Hadley 的建议使用 @export,这会导致 NAMESPACE 中的 S3method 条目正确,并且包通过了所有检查 -as-cran。但是,这些函数未在 NAMESPACE 中导出,因此未找到调用 print.myclass(我理解这是避免混乱 NAMESPACE 所需的行为)。但是,通过 Mypackage::print.myclass 调用该函数也会导致该函数不是从 导出的对象的错误Mypackage

问题:这是正确的行为吗?或者是否需要其他步骤才能导出该函数?我曾尝试添加 @method print Myclass 和 @export 但没有运气。devtools在 MAC OS X 10.12.6 下使用 R 3.4.2 和1.13.3

谢谢!梅丽丝

编辑:更新为具有将添加/导出方法和导出功能的代码

简单示例 - 在 RStudio 中构建一个具有以下功能的骨架包:

#' test for export of S3 methods
#'
#' @title "print hello world for any object"
#' @param x object
#' @param digits optional number specifying the number of digits to display
#' @param ... other parameters to be passed to \code{print.default}
#' @export print.hello
#' @export
print.hello = function (x, digits = max(3, getOption("digits") - 3), ...)
{
  cat("\n Hello World \n")
  invisible()
}
Run Code Online (Sandbox Code Playgroud)

NAMESPACE 现在有

# Generated by roxygen2: do not edit by hand

S3method(print,hello)
export(print.hello)
Run Code Online (Sandbox Code Playgroud)

使用不带参数的@export 导出方法,而@export print.hello 导出函数,但不会将该方法添加到NAMESPACE(这会导致包检查错误)。两者都允许导出方法和函数。

Luk*_*ney 2

这是您的文件的正确行为NAMESPACE::访问导出的变量,因此testPackage::print.hello应该失败。 :::访问内部变量,所以testPackage:::print.hello应该适合你。