在不引入依赖关系的情况下,在 R 包中定义 S3 方法的首选方法是什么?

Nic*_*edy 5 r project r-s3 roxygen2

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

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

duc*_*ayr 6

幸运的是,对于 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)
#' }
Run Code Online (Sandbox Code Playgroud)

所以,你只需要简单地使用@importFrom和替代@export,使用@exportS3Method package::generic(见https://github.com/r-lib/roxygen2/issues/796https://github.com/r-lib/roxygen2/commit/ 843432ddc05bc2dabc9b5b22c1ae7de507a00508 )

插图

所以,为了说明,我们可以制作两个非常简单的包,foobar. 该包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.")
}
Run Code Online (Sandbox Code Playgroud)

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

create_package("bar")
Run Code Online (Sandbox Code Playgroud)

它创建了一个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.")
}
Run Code Online (Sandbox Code Playgroud)

重要的是,我们必须 加载foo运行前,包document(),或@exportS3Method将无法正常工作。那是,

library(foo)
document()
Run Code Online (Sandbox Code Playgroud)

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

# Generated by roxygen2: do not edit by hand

S3method(foo::foo,bar)
Run Code Online (Sandbox Code Playgroud)

我们必须手动添加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)
Run Code Online (Sandbox Code Playgroud)