在R中安全地创建S3泛型 [英] Safely creating S3 Generics in R

查看:128
本文介绍了在R中安全地创建S3泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Henrik Bengtsson为互联网提供了一种很好的在R 中创建S3泛型的方法,打扰他们是否已经在2002年之前创建过。

他的功能集GenericsS3的功能基本上是:


  • 检查名称是否正常

  • 检查是否存在具有该名称的函数



如果是这样,


  • 检查它是否为通用的

  • 如果不是,则将其重命名为.default并创建一个通用



如果不是,只需创建泛型。

这个代码证明在自己的包中没有可用时自动创建泛型非常有用。当我们走过这个R时代的时候,我想知道现在在R中实现相同的方法是什么。我在R中找不到 isS3Generic()或类似的函数,并且Henrik Bengtsson的代码早在R 2.14中引入的强制名称空间之前。我记得我已经看到了其他实现相同的方法,但找不到它们。编辑:我专门寻找S3。函数 isGeneric()仅适用于S4,例如anova(这是一个S3泛型):

 > isGeneric('anova')
[1] FALSE
> anova
函数(object,...)
UseMethod(anova)
<字节码:0x04dc7a18>
< environment:namespace:stats>


解决方案

您可能会发现有助于查看 R.oo包裹


Henrik Bengtsson has provided the internet with a nice way of creating S3 generics in R without having to bother whether they were already created before... in 2002.

What his function setGenericsS3 does, is basically:

  • check whether the name is fine
  • check whether there is a function with that name

if so,

  • check whether it is a generic
  • in case it's not, rename it as .default and create a generic

if not, just create the generic.

This code proved very useful to automatically create generics when there was none available in your own packages. As we moved quite past this R era, I was wondering what the correct way is to achieve the same in R now. I can't find an isS3Generic() or similar function in R, and the code of Henrik Bengtsson originates from long before the obligatory namespaces as introduced in R 2.14. I remember I've seen other ways of achieving the same, but can't locate them.

EDIT : I'm specifically looking for S3. The function isGeneric() only works for S4, eg for anova (which is a S3 generic) :

> isGeneric('anova')
[1] FALSE
> anova
function (object, ...) 
UseMethod("anova")
<bytecode: 0x04dc7a18>
<environment: namespace:stats>

解决方案

You might find useful to look into the R.oo package

这篇关于在R中安全地创建S3泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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