Skip to content
Prev 2761 / 12125 Next

[R-pkg-devel] Creating S3 methods for S4 classes

Hi Joris,

At least some aspects of this topic are of interest on this list, given its complexity.

I think that your example:

setGeneric("predict")
setMethod("predict", "ANY", stats::predict)

creates a new function 'predict' in your namespace, it doesn't make stats::predict S4 generic. This is fine when your 'predict' is visible
but  when it is not, stats::predict will be called. 

So, for classes for which you define methods for predict, what happens may depend on whether or not your package is attached. 
On the other hand, S3 methods defined for predict, will be used (if exported) as long as  the package is attached. 

I may be wrong but for this or similar reasons I believe that 'stats4' needs to be in "Depends" to ensure that it shadows the base functions it redefines. 

Georgi Boshnakov


====================================
function (object, ...) 
UseMethod("coef")
<bytecode: 0x000000000a844730>
<environment: namespace:stats>
standardGeneric for "coef" defined from package "stats"

function (object, ...) 
standardGeneric("coef")
<environment: 0x000000000b5ddcb8>
Methods may be defined for arguments: object
Use  showMethods("coef")  for currently available ones.

===================================



-----Original Message-----
From: R-package-devel [mailto:r-package-devel-bounces at r-project.org] On Behalf Of Georgi Boshnakov
Sent: 24 May 2018 15:38
To: Joris.Meys at ugent.be; R Package Development
Subject: Re: [R-pkg-devel] Creating S3 methods for S4 classes

My understanding is that the  S3 methods guard against situations where only S3 dispatch is done,
e.g. by some internal generics. It may well be that for 'predict' this is not theoretical possibility.

I second your suggestion about predict and stats4. I have wondered in the past why 'predict' has been left out of stats4. 

Georgi Boshnakov




-----Original Message-----
From: R-package-devel [mailto:r-package-devel-bounces at r-project.org] On Behalf Of Joris Meys
Sent: 24 May 2018 13:17
To: R Package Development
Subject: [R-pkg-devel] Creating S3 methods for S4 classes

Dear all,

per the manual, one should create and register both the S3 and a S4 method
if one needs a method for an S4 class for a function using S3 dispatching.
This is cumbersome, and not very optimal.

I was wondering if there's a better way to do this. Currently I recreate a
generic in my package and create a default method that sends all the other
classes to the S3 generic, eg:

setGeneric("predict")
setMethod("predict", "ANY", stats::predict)

I'm not sure if this hasn't any adverse consequences, as it is not the
recommended approach

It would be great if these generics could be made available through stats4.
If this would be the prefered route, I volunteer to create the patch for
that.

Any thoughts?
Cheers
Joris