R:何时在命名空间中使用setGeneric或导出s4方法 [英] R: when to use setGeneric or export a s4 method in the namespace

查看:329
本文介绍了R:何时在命名空间中使用setGeneric或导出s4方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型R包,以期将来将其提交给Bioconductor,这就是为什么我决定尝试s4类的原因.不幸的是,我在了解何时应该在程序包中不使用setGeneric时遇到了问题,并且setGeneric方法的文档对我来说或多或少令人难以理解.

I am writing a small R package with the idea to submit it to Bioconductor in the future, which is why I decided to try out s4 classes. Unfortunately I had problems understanding when I should use setGeneric or not in my package, and the documentation for the setGeneric method is for me more or less incomprehensible.

具体示例:

  1. 我创建了一个名为 Foo
  2. 的s4类
  3. 我使用setMethod("[","Foo", ...)
  4. [<-运算符定义了一种方法
  5. 我使用setMethod("as.list", "Foo",...)
  6. 定义了as.list函数的方法
  7. 我避免使用setGenerics并在命名空间中导出我的方法,因为我读到了已定义的泛型函数不需要的地方
  1. I created a s4 class called Foo
  2. I defined a method for the [<- operator using setMethod("[","Foo", ...)
  3. I defined a method for the as.list function using setMethod("as.list", "Foo",...)
  4. I avoided using setGenerics and exporting my methods in the namespace as I read somewhere that it's not needed for already defined generic functions

现在的问题是[访问器方法像超级按钮一样工作,但是as.list无法正常工作.更令人困惑的是,当我通过在R终端上键入library(BiocGenerics)导入库 BiocGenerics 时,as.list开始起作用.

The problem now is that the [ accessor method works like a charm, but as.list is not working. Even more confusing, when I import the library BiocGenerics by typing library(BiocGenerics) at the R terminal, as.list starts to work.

问题1:如何确定[将始终有效?这不仅仅是偶然,因为我导入了一些库吗?

Question 1: how can I be sure that [ will always work? And it is not just a coincidence because I imported some libraries?

问题2:如何使as.list工作?在命名空间中导出方法?使用setGeneric?

Question 2: what should I do to make as.list work? Export the method in the namespace? use setGeneric?

问题3:我认为as.list开始起作用是因为在BiocGenerics软件包中使用了setGeneric("as.list"...),但事实并非如此,请从此处阅读:

Question 3: I thought that as.list started to work because setGeneric("as.list"...) was used in the BiocGenerics package, but this does not seem to be the case, reading from here: http://www.bioconductor.org/packages/release/bioc/manuals/BiocGenerics/man/BiocGenerics.pdf
So why did as.list start to work? Where was it defined?

推荐答案

在仅加载基本包和方法的R会话中,您可以看到已定义"["的泛型,以及"as.list"的泛型不是

In an R session with only the base packages and methods loaded you can see that the generic for "[" is defined, and the generic for "as.list" is not

> getGeneric("[")
standardGeneric for "[" defined from package "base"

function (x, i, j, ..., drop = TRUE) 
standardGeneric("[", .Primitive("["))
<bytecode: 0x28f7990>
<environment: 0x28ebef0>
Methods may be defined for arguments: x, i, j, drop
Use  showMethods("[")  for currently available ones.
> getGeneric("as.list")
NULL

加载BiocGenerics确实为as.list定义了泛型

Loading BiocGenerics does define the generic for as.list

> suppressPackageStartupMessages(library(BiocGenerics))
> getGeneric("as.list")
standardGeneric for "as.list" defined from package "base"

function (x, ...) 
standardGeneric("as.list")
<environment: 0x5969210>
Methods may be defined for arguments: x
Use  showMethods("as.list")  for currently available ones.

如果您的包装预定用于Bioconductor,请重新使用BiocGenerics的仿制药.在您的NAMESPACE文件中执行以下操作

If your package is destined for use in Bioconductor, then re-use the generic from BiocGenerics. Do this with the following in your NAMESPACE file

import(methods)
import(BiocGenerics)

exportMethods("[", "as.list")

,并在说明文件中包含以下内容

and with the following in your DESCRIPTION file

Imports: methods, BiocGenerics

如果您不从BiocGenerics导入仿制药,则您的setMethod("as.list", ...)将创建它自己的仿制药.在Bioconductor中,用户必须说YourPkg::as.list(YourObject)BiocGenerics::as.list(NotYourObject)显然不是一个好主意.

If you do not import the generic from BiocGenerics, then your setMethod("as.list", ...) will create it's own generic. In the context of Bioconductor, a user would have to say YourPkg::as.list(YourObject) or BiocGenerics::as.list(NotYourObject) which is clearly not a good idea.

您需要import(methods),因为在您的交互式试用中,您依赖于method包位于search()路径上.但是(a)R CMD BATCH在运行时未附加方法包,并且(b)最佳实践是依赖在您的名称空间中显式 的符号,而不是仅依赖于搜索路径(因此可能会被其他程序包或具有任意行为的用户功能掩盖.这与其他方法包一样适用.

You need to import(methods) because in your interactive trials you're relying on the methods package to be on the search() path. But (a) R CMD BATCH runs without the methods package attached and (b) it is best practice to rely on symbols that are explicitly in your name space, rather than merely on the search path (and hence subject to masking by other packages or user functions with arbitrary behavior. This applies as much to the methods package as any other.

这篇关于R:何时在命名空间中使用setGeneric或导出s4方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆