Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safely creating S3 Generics in R

Tags:

r

generics

r-s3

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>
like image 247
Joris Meys Avatar asked Dec 12 '11 13:12

Joris Meys


1 Answers

You can use isGenericS3 function of the R.methodsS3 package. Please see the code below:

library(R.methodsS3)
isGenericS3(anova)
# [1] TRUE
like image 90
Artem Avatar answered Oct 23 '22 07:10

Artem