7

我有一个 R 包(目前不在 CRAN 上),它定义了一些来自其他包(特别是knitr::knit_printhuxtable::as_huxtable)的通用函数的 S3 方法。但是,它们不是我的包的关键部分,所以我不想在用户安装我的包时创建对这些包的依赖。直到 R 4.0.0,我导出了 S3 方法而不导入泛型。使用roxygen2,我的@export指令被翻译成export()NAMESPACE 中的指令,而不是S3method(). 这在 R 版本 < 4.0.0 中运行良好,因为 Rgeneric_function.class首先在全局环境中查找匹配方法,而不是依赖于 S3 方法的正确注册。但是,根据developer.r-project.org 上的这篇博客,R 不再寻找未注册的 S3 方法。

解决这个问题的最好方法是什么?现在,我已将@importFrom指令添加到我的roxygen2块中,并将两个包添加到说明的导入部分。但是,据我了解,这意味着任何安装我的软件包的用户也必须安装knitrhuxtable无论他们是否愿意。

4

2 回答 2

8

幸运的是,对于 R >= 3.6.0,您甚至不需要caldwellst 的答案。从您在上面链接的博客条目中:

从 R 3.6.0 开始,NAMESPACE 中的 S3method() 指令也可用于执行延迟的 S3 方法注册。使用 S3method(PKG::GEN, CLS, FUN) 函数,只有在加载 PKG 的命名空间时,FUN 才会从包 PKG 中注册为 CLS 类和通用 GEN 的 S3 方法。这可以用于处理不“立即”需要该方法的情况,并且必须预加载 pkg 的命名空间(及其所有强依赖项)以执行立即注册被认为过于“昂贵”。

此外,这也在文档中讨论了其他建议vctrs::s3_register()

#' For R 3.5.0 and later, `s3_register()` is also useful when demonstrating
#' class creation in a vignette, since method lookup no longer always involves
#' the lexical scope. For R 3.6.0 and later, you can achieve a similar effect
#' by using "delayed method registration", i.e. placing the following in your
#' `NAMESPACE` file:
#'
#' ```
#' if (getRversion() >= "3.6.0") {
#'   S3method(package::generic, class)
#' }

因此,您只需要不使用and@importFrom而不是@export, 使用@exportS3Method package::generic(参见https://github.com/r-lib/roxygen2/issues/796https://github.com/r-lib/roxygen2/commit/ 843432ddc05bc2dabc9b5b22c1ae7de507a00508 )

插图

因此,为了说明,我们可以制作两个非常简单的包,foo并且bar. 该包foo只有一个通用foo()函数和默认方法:

library(devtools)
create_package("foo")

#' foo generic
#'
#' @param x An object
#' @param ... Arguments passed to or from other methods
#' @export
foo <- function(x, ...) {
    UseMethod("foo", x)
}
#' foo default method
#'
#' @param x An object
#' @param ... Arguments passed to or from other methods
#' @export
foo.default <- function(x, ...) {
    print("Called default method for foo.")
}

之后document()install()我们创建bar

create_package("bar")

它创建了一个bar方法foo()

#' bar method for foo
#'
#' @param x A bar object
#' @param ... Arguments passed to or from other methods
#'
#' @exportS3Method foo::foo
foo.bar <- function(x, ...) {
    print("Called bar method for foo.")
}

重要的是,我们必须 在运行之前加载foodocument(),否则@exportS3Method将无法工作。那是,

library(foo)
document()

但是,如果我们这样做,我们会在NAMESPACEfor中得到以下内容bar

# Generated by roxygen2: do not edit by hand

S3method(foo::foo,bar)

我们必须手动添加foo到“建议”中DESCRIPTION

然后如果我们卸载foo,我们仍然可以安装bar

> remove.packages("foo")
Removing package from ‘/home/duckmayr/R/x86_64-pc-linux-gnu-library/4.0’
(as ‘lib’ is unspecified)
> install("bar")
✓  checking for file ‘/home/jb/bar/DESCRIPTION’ ...
─  preparing ‘bar’:
✓  checking DESCRIPTION meta-information ...
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
─  building ‘bar_0.0.0.9000.tar.gz’

Running /opt/R/4.0.0/lib/R/bin/R CMD INSTALL \
  /tmp/Rtmp5Xgwqf/bar_0.0.0.9000.tar.gz --install-tests 
* installing to library ‘/home/jb/R/x86_64-pc-linux-gnu-library/4.0’
* installing *source* package ‘bar’ ...
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (bar)
于 2020-04-28T15:25:55.347 回答
2

vctrs包提供了一个名为的函数,该函数s3_register动态注册用于该.onLoad函数的方法。您可以在此处阅读有关其使用的更多信息,您自己会想要:

.onLoad <- function(...) {
  if (requireNamespace("knitr", quietly = TRUE)) {
    vctrs::s3_register("knitr::knit_print", "class_name")
  }
  if (requireNamespace("huxtable", quietly = TRUE)) {
    vctrs::s3_register("huxtable::as_huxtable", "class_name")
  }
}

该文档也很友好,因此您不必导入vctrs

为了避免这个函数依赖于 vctrs,请随意将函数源复制并粘贴到您自己的包中。

于 2020-04-28T15:10:44.823 回答