Re: [Rd] format: different S4 behavior in a package

From: Daniel Murphy <chiefmurphy_at_gmail.com>
Date: Wed, 23 Jun 2010 08:03:34 -0700

Thank you, Martin. That worked. And fyi 'export(format)' alone was not sufficient.
Best,
Dan

On Wed, Jun 23, 2010 at 5:15 AM, Martin Morgan <mtmorgan_at_fhcrc.org> wrote:

> On 06/23/2010 12:16 AM, 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"
>
> This is the clue -- you're creating a new S4 generic, so there's a
> base::format, and a .GlobalEnv::format. Test::formatMe respects its name
> space, and sees base::format.
>
> In the S3 case, base::format is already an S3 generic, and you're just
> adding a method, so there's only base::format for everyone to find.
>
> In Test, you could setGeneric(format) and then export(format). It might
> also be enough to just export(format); I'm not sure.
>
> Martin
>
> > [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
>
>
> --
> Martin Morgan
> Computational Biology / Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N.
> PO Box 19024 Seattle, WA 98109
>
> Location: Arnold Building M1 B861
> Phone: (206) 667-2793
>

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 23 Jun 2010 - 15:06:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Jun 2010 - 07:51:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive