From Giles.Heywood at CommerzbankIB.com  Thu Jan  8 16:44:55 2004
From: Giles.Heywood at CommerzbankIB.com (Heywood, Giles)
Date: Thu Jan  8 16:45:10 2004
Subject: [Rd] S3, S4, namespace
Message-ID: <8CBAA121CEB4D5118CB200508BB2BBEF05BF73F7@xmx8lonib.lonib.commerzbank.com>

Thanks for the tips so far.  I have modified the example so that 

(a) I do not define an S4 method for print [I use summary instead, which
gets similar treatment in mle as far as I can see]

(b) I do not do use the 'import(methods)' directive  [I previously did this
because John Chambers explicitly and as point number 1 stated that this was
required, in his email dated 6th September - actually I seem to get the same
results either way]

Nevertheless I get the same behaviour when trying to access a generic method
defined in one package [barpkg in my example] and imported into a second
package [wazpkg in my example].  If I require() the package I get the
behaviour I expect.

- Giles

#the first bit which is fine------------------

#the .R file for package barpkg

setClass("bar",representation("numeric"))

setMethod("summary",signature(object="bar"),
    function(object,...)
        {
        print("summary method for bar")
        summary(object@.Data,...)
        }
    )

setGeneric("barprint",useAsDefault=print)
setMethod("barprint",signature(x="bar"),
    function(x,...)
        {
        print("barprint method for bar")
        print(x@.Data,...)
        }
    )

#the NAMESPACE file for package barpkg

exportMethods(barprint,summary)
exportClasses("bar")

#in R

> require(barpkg)
Loading required package: barpkg 
[1] TRUE
> x <- new("bar",1:5)
> summary(x)
[1] "summary method for bar"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      1       2       3       3       4       5 
> barprint(x)
[1] "barprint method for bar"
[1] 1 2 3 4 5
>

#now the bit which is has been puzzling to me ------------

#the .R file for package wazpkg

wazsummary <- function(x)
    {
    summary(new("bar",x))
    }

wazbarprint <- function(x)
    {
    barprint(new("bar",x))
    }

#the NAMESPACE file for package wazpkg

import(barpkg)
importMethodsFrom(barpkg)
importClassesFrom(barpkg)
export(wazsummary,wazbarprint)

#in R

> require(wazpkg)
Loading required package: wazpkg 
[1] TRUE
> wazbarprint(1:5)
[1] "barprint method for bar"
[1] 1 2 3 4 5
> wazsummary(1:5)
[1] "summary method for bar"
Error in .setMethodsForDispatch(f, fdef, resetMlist) : 
        Internal error: did not get a valid generic function object for
function "summary"
Error in summary(object@.Data, ...) : S language method selection got an
error when called from internal dispatch for function "summary"
> 

#HOWEVER... -----------------------

> require(barpkg)
Loading required package: barpkg 
[1] TRUE
> require(wazpkg)
[1] TRUE
> wazbarprint(1:5)
[1] "barprint method for bar"
[1] 1 2 3 4 5
> wazsummary(1:5)
[1] "summary method for bar"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      1       2       3       3       4       5 


-----------------------------

platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    1              
minor    8.1            
year     2003           
month    11             
day      21             
language R  

I use the following:

Rcmd install --doc="none"  --save wazpkg


********************************************************************** 
This is a commercial communication from Commerzbank AG.\ \ T...{{dropped}}


[Rd] S3, S4, namespace
Heywood, Giles