[R] S3 and S4 clash

Spencer Graves spencer.graves at pdf.com
Wed Dec 12 17:32:35 CET 2007


Dear Prof. Ripley: 

      Thanks for your help.  I very much appreciate your great 
generosity in both your substantive contributions to the code and 
documentation for R and in responding to so many questions, helping to 
make R what it is today. 

      In the real application that led to the toy example below, I had 
defined

            AIC.factanal <- function(object, ..., k=c(2, "BIC")){ ... }

      1.  If I understand your suggestion, I'd be better NOT defining an 
"AIC.factanal" but instead define only "logLik.factanal" and let methods 
dispatch go through "AIC.default" to "logLik.factanal".  Is this correct? 

      2.  Is it inappropriate to supply other optional values for an 
argument in a generic function like this? 

      3.  If it is OK to specify options for "k" like this, how would 
you suggest I do it?  Use the S4 standard? 

      Thanks again. 
      Spencer Graves

Prof Brian Ripley wrote:
> I'd say that was pretty clearly a bug in stats4 (which as I recall was 
> needed to get around the scoping awkwardnesses of S4).
>
> But could you not write a logLik method for your class?  E.g.
>
>> logLik.bar <- function(object, ...) structure(pi, class="logLik", df=1)
>> AIC(bar.tmp)
> [1] -4.283185
>> library(stats4)
>> AIC(bar.tmp)
> [1] -4.283185
>
>
> On Thu, 6 Dec 2007, Spencer Graves wrote:
>
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>



More information about the R-help mailing list