From chalabi at phys.ethz.ch  Wed Mar 11 21:44:24 2009
From: chalabi at phys.ethz.ch (Yohan Chalabi)
Date: Wed, 11 Mar 2009 21:44:24 +0100
Subject: [Rd] Could you please add "time<-" as a generic function in the
 'stats' package ?
In-Reply-To: <49B7ED87.3090102@r-project.org>
References: <20090311145557.5c9b57e0@mimi>
	<49B7ED87.3090102@r-project.org>
Message-ID: <20090311214424.558b4dff@mimi>

>>>> "JC" == John Chambers <jmc at r-project.org>
>>>> on Wed, 11 Mar 2009 09:57:43 -0700

   JC> Whatever one wants for an S3 generic, it's not needed to do what,
   JC> presumably, you want here.
   JC>
   JC> And for sure it is no excuse for S3 methods for S4 classes.
   JC>
   JC> Back to basics: To write S4 methods for an existing function, the clean
   JC> and simple way is usually:
   JC>
   JC> setGeneric("time<-")
   JC>
   JC> If your package depends on one that has S3  methods for this function,
   JC> there will be a version of the function imported into your namespace.
   JC> That function will then be the default method.
   JC>
   JC> Presumably you want to ensure that S3 methods, for S3 classes, are still
   JC> dispatched.  Quite reasonable and it should follow from the call to
   JC> setGeneric.
   JC>
   JC> If you wanted to have your own S3 methods or if you weren't willing to
   JC> assume an S3 generic imported, you could do a 2-line version:
   JC>
   JC> R(r48103)> `time<-` <- function(x, value) UseMethod("time<-")
   JC> R(r48103)> setGeneric("time<-")
   JC> [1] "time<-"
   JC> R(r48103)> showMethods("time<-", include = TRUE)
   JC> Function: time<- (package .GlobalEnv)
   JC> x="ANY"
   JC> function (x, value)
   JC> UseMethod("time<-")

In my opinion you example only works in '.GlobalEnv' environement, but
does not work when implemented in a package.

I wrote a small package to illustrate the problem.

## R code
oldDir <- getwd()
setwd(tempdir())

url <- "http://nic.phys.ethz.ch/~chalabi/timeProb_0.001.tar.gz"
download.file(url, "timeProb_0.001.tar.gz")
install.packages("timeProb_0.001.tar.gz", repos = NULL)

# you also need to install another package which uses `time<-`.
# for example zoo
if(!require(zoo))
    install.packages("zoo")

# Now we quit and start a new R session
setwd(oldDir)
q()

# new R

# we first load timeProb
library(timeProb)

# and then run the demo
demo(timeProb)

# now we quit and start again a new session
q()

# new R

# but now we first load zoo and after timeProb
library(zoo)
library(timeProb)

# and run again the demo
demo(timeProb)

## end R code

As far as I understand it, two packages can not coexist if one of
them defines an S3 generic and the other one defines an S4 generic
with the same name.

Or am I missing an option when defining the S4 generic?

   JC>
   JC> As a postscript, here is the current plan, not yet committed, pending
   JC> some more testing:
   JC>   - the bad methods will be allowed
   JC>   - warnings when a class is defined with such methods for a superclass
   JC>   - probably some other warnings, but not for an ordinary call to the
   JC> method (it's the MISSING calls to the method that are the disaster).
   JC>

Thanks for giving more information concerning the current plan.

   JC> More later,
   JC>   John
   JC>


regards,
Yohan

--
PhD student
Swiss Federal Institute of Technology
Zurich

www.ethz.ch



[Rd] Could you please add "time<-" as a generic function in the 'stats' package ?
Yohan Chalabi