[R] S3 and S4 clash

Spencer Graves spencer.graves at pdf.com
Thu Dec 6 21:18:27 CET 2007


Hello: 

      How can I work around the conflict between the S3 and S4 
illustrated in the example below?  I'm developing a package that 
requires a function in 'stats4', but when 'stats4' is attached, it 
breaks my AIC function.  I could give my AIC function another name so it 
no longer uses the generic dispatch, but I wonder if there is another way. 

      Thanks,
      Spencer Graves
################################
bar.tmp <- structure(1, class = "bar")
AIC.bar <- function(object, ..., k=2) {
  3
}

 > AIC(bar.tmp)
[1] 3
 > library(stats4)
 > AIC(bar.tmp)
Error in UseMethod("logLik") : no applicable method for "logLik"
 > detach("package:stats4")
 > AIC(bar.tmp)
[1] 3
 > objects()
[1] "AIC.bar" "bar.tmp"
 > sessionInfo()
R version 2.6.1 (2007-11-26)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] stats4_2.6.1



More information about the R-help mailing list