From mtmorgan at fhcrc.org  Wed Jun 23 14:15:17 2010
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Wed, 23 Jun 2010 05:15:17 -0700
Subject: [Rd] format: different S4 behavior in a package
In-Reply-To: <AANLkTinVWv3MMb1KcHkNTuEA_W8utTYkItRw3whz1Lkc@mail.gmail.com>
References: <AANLkTinVWv3MMb1KcHkNTuEA_W8utTYkItRw3whz1Lkc@mail.gmail.com>
Message-ID: <4C21FAD5.6050806@fhcrc.org>

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



[Rd] format: different S4 behavior in a package
Martin Morgan