[Rd] S3, S4, namespace

Heywood, Giles Giles.Heywood at CommerzbankIB.com
Thu Jan 8 12:06:33 MET 2004


I have encountered an issue which I have been unable to resolve, involving
an S3 generic (print) being declared S4 generic in a package, and the method
being exported.  This all works fine - the problem occurs when I try to
import the method to another package.

Here is the bit that works fine. -------------

#the .r file for package bar

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

if(!isGeneric("print")) {setGeneric("print",useAsDefault=print)}
setMethod("print",signature(x="bar"),
    print.bar <- function(x,...)
        {
        print("print method for bar")
        print(x at .Data,...)
        }
    )

if(!isGeneric("barprint")) {setGeneric("barprint",useAsDefault=print)}
setMethod("barprint",signature(x="bar"),
    printBar <- function(x,...)
        {
        print("barprint method for bar")
        print(x at .Data,...)
        }
    )

#the NAMESPACE file for package bar

import(methods)
exportMethods(barprint,print)
exportClasses("bar")

#then in R:

> require(barpkg)
Loading required package: barpkg 
[1] TRUE
> x <- new("bar",99)
> print(x)
[1] "print method for bar"
[1] 99
> barprint(x)
[1] "barprint method for bar"
[1] 99

Fine.

Here is the bit that I have not figured out. -------------

#the .r file for package waz

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

#the NAMESPACE file for package waz

import(methods,barpkg)
importMethodsFrom(barpkg)
importClassesFrom(barpkg)
export(wazbar)

#then in R

> require(wazpkg)
Loading required package: wazpkg 
[1] TRUE
> wazbar(99)
Error in .setMethodsForDispatch(f, fdef, resetMlist) : 
        Internal error: did not get a valid generic function object for
function "print"
Error in print("barprint method for bar") : 
        S language method selection got an error when called from internal
dispatch for function "print"

end of R bit ---------------------------

The problem goes away if I don't set print as an S4 generic in bar.

Can anyone help - maybe an error is obvious?  

As a bit of background, I am package maintainer for its (Irregular
Time-Series), and have been obliged to put it in a namespace or suffer a
significant performance penalty (about 6x slower).  In the package I make S3
generics (plot, print, summary etc) S4 generic in this package, and so far
have not got into trouble for doing so.  However, I have now got stuck -
possibly I have not seen the right documentation (I refer to the newsletter
article, and John Chambers' correspondence on R-devel early September).

- Giles

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           

Rcmd install --doc="none"  barpkg


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



More information about the R-devel mailing list