[Rd] format: different S4 behavior in a package

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Jun 23 09:33:39 CEST 2010


There is not a single definition of
formatMe()
in your message, therefore nothing works for me, not even the very first 
version...

Best,
Uwe Ligges


On 23.06.2010 09:16, Daniel Murphy wrote:
> R-Devel-ers:
>
> I have an S4 method that simply formats an object:
>
> setGeneric("formatMe", function(x) standardGeneric("formatMe"))
> setMethod("formatMe", "ANY", function(x) format(x))
>
> If I issue the above in an R session, then define an S4 class with its own
> format method, I get the desired result:
>
>> setClass("A",contains="numeric")
> [1] "A"
>> setMethod("format","A", function(x, ...) "Hey Jude")
> Creating a new generic function for "format" in ".GlobalEnv"
> [1] "format"
>> a<-new("A",1968)
>> formatMe(a)
> [1] "Hey Jude"
>
>
> However, if I put the two "formatMe" definitions into a package ("Test"), I
> do not get the desired result.
> <start new R session>
>
>> library(Test)
>> setClass("A",contains="numeric")
> [1] "A"
>> setMethod("format","A", function(x, ...) "Hey Jude")
> Creating a new generic function for "format" in ".GlobalEnv"
> [1] "format"
>> a<-new("A",1968)
>> formatMe(a)
> [1] "1968"
>
>
> The "disconnect" does not occur, however, if the S4 format method is an S3
> incarnation:
>
>> setClass("B",contains="numeric",S3methods=TRUE)
> [1] "B"
>> format.B<- function(x, ...) "Don't make it bad"
>> b<-new("B",1968)
>> formatMe(b)
> [1] "Don't make it bad"
>
> Could the problem be in Test's NAMESPACE file? There is only one line:
> exportMethods(formatMe)
>
> Here is Test's DESCRIPTION file:
> Package: Test
> Type: Package
> Title: Testing format
> Version: 1.0
> Date: 2010-06-22
> Author: Dan Murphy
> Maintainer: Dan Murphy<snipped>
> Depends: methods
> Description: Does format in a package work with S4 format method?
> License: GPL (>= 2)
> LazyLoad: yes
>
> (I would send the Help file, but I don't think that is the problem.)
>
> I am using version 2.11.1 on a Windows Vista machine.
>
> Any guidance would be appreciated. Thank you
>
> Dan Murphy
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list