本文介绍了在R中,我怎样才能从另一个包中扩展泛型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 PackageA 的通用函数:

  #'doWork 
#'
#'做一些工作!
#'
#'@export
setGeneric(
doWork,
function(x){

standardGeneric(doWork )
$)

setMethod(
doWork,
signature = c(numeric),
definition = function(x){

x == 10#某些逻辑...
}

PackageB 中,它依赖于 PackageA ,我想在 doWork

 #'@import PackageA 
setMethod(
doWork,
signature = c(character),
definition = function(x){

length(x)== 1&& x x ==10#一些更多的逻辑...
}

这是有效的,但这意味着用户 PackageB 还必须 library(PackageA)



这会失败:

  library(PackageB)

doWork(10)#Fails!

这是可行的:

  library(PackageA)
library(PackageB)

doWork(10)

我想使用 PackageA 包含在 PackageB 中,但不要求 PackageA 方法在 PackageB 中。



我该如何做到这一点?

解决方案

这实际上是有文件记载的,但并不十分清楚;参见。

诀窍是从 PackageA 导入泛型,然后将其从 PackageB 。使用 roxygen 注解,这看起来像:

 #'@importMethodsFrom PackageA doWork 
#'@export
setMethod(
doWork,
signature = c(character),
definition = function(x){

length(x)== 1&& x ==10#更多逻辑...
})
$ b

调用 devtools :: document()时,除非先载入 PackageA (调用 library(PackageA))。然而,一旦构建,不需要 PackageA

 >库(PackageB)
> showMethods(doWork)
功能:doWork(package PackageA)
x =character
x =numeric

作为参考,自动生成的 NAMESPACE 文件如下所示:

  exportMethods(doWork)
importMethodsFrom(PackageA,doWork)

这种方法不会产生关于命名冲突等的警告,所以它似乎是犹太教。

I have a package PackageA with a generic function:

#' doWork
#' 
#' Do some work!
#'
#' @export
setGeneric(
    "doWork", 
    function(x) {

        standardGeneric("doWork")
    })

setMethod(
    "doWork", 
    signature = c("numeric"), 
    definition = function(x) {

        x == 10 # Some logic... 
    }

In PackageB, which depends on PackageA, I would like to add more methods to doWork:

#' @import PackageA
setMethod(
    "doWork", 
    signature = c("character"), 
    definition = function(x) {

        length(x) == 1 && x == "10" # Some more logic... 
    }

This works. However, it means that the user of PackageB must also library(PackageA).

This fails:

library(PackageB)

doWork("10") # Fails!

This works:

library(PackageA)
library(PackageB)

doWork("10")

I would like to use the generic from PackageA in PackageB, but not require PackageA to be loaded to use just the methods in PackageB.

How can I achieve this?

解决方案

This is actually documented, but it's not very clear; see section 1.5.6 of Writing R Extensions.

The trick is to import the generic from PackageA and then re-export it from PackageB. Using roxygen annotations, this looks like:

#' @importMethodsFrom PackageA doWork
#' @export 
setMethod(
    "doWork", 
    signature = c("character"), 
    definition = function(x) {

        length(x) == 1 && x == "10" # Some more logic... 
    })

When you call devtools::document(), this will fail unless you have first loaded PackageA (call library(PackageA)).

However, once built, PackageA is not required:

> library(PackageB)
> showMethods("doWork")
Function: doWork (package PackageA)
x="character"
x="numeric"

For reference, the auto-generated NAMESPACE file looks like:

exportMethods(doWork)
importMethodsFrom(PackageA, doWork)

This method produces no warnings about naming conflicts and so on, so it seems to be "kosher".

这篇关于在R中,我怎样才能从另一个包中扩展泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:04