Adam Gower
Adam Gower

Reputation: 11

How to use/maintain my S4 class with same name as S4 class in another R package?

How to use/maintain my S4 class with same name as S4 class in another R package?

I'm writing a large R package that includes an S4 class named 'FeatureSet'. Unfortunately, this is also the name of a virtual S4 class in the widely used 'oligo' package. When I load my package first, and then oligo, and call

new("FeatureSet")

I get the error

Error in new("FeatureSet") :
  trying to generate an object from a virtual class ("FeatureSet")

In the NAMESPACE file, I've tried including 'exportClasses' and 'exportClassPattern' to export this S4 definition, or all S4 definitions, from my package without luck. I've also included

exportMethods(coerce, initialize, show)

as I've defined methods for these generics in my package.

Is there a best-practice way to write R code to recognize only a specific S4 definition when multiple definitions with the same name are attached? I could, of course, give my class a different name, but there must be some way to make sure that my classes don't collide with those in some other package in the future. I've spent quite a while looking through the usual forums and documentation and was surprised to see that this question was not answered anywhere. Thanks in advance!

Upvotes: 1

Views: 148

Answers (1)

Martin Morgan
Martin Morgan

Reputation: 46866

I think the best practice is to choose a different, more descriptive name. It is inherently confusing to have two objects of the same name but different structure.

You can construct an instance of 'your' class via

new(getClass("FeatureSet", where=getNamespace("YourPackage")))`

probably wrapped in a simple 'constructor' function.

While in principle the methods package 'knows' the difference between an instance of your class and a class of the same name from another package, you are almost certainly going to run into implementation bugs that will frustrate you and your users.

Upvotes: 2

Related Questions