From mtmorgan at fhcrc.org  Wed Feb  3 04:02:34 2010
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Tue, 02 Feb 2010 19:02:34 -0800
Subject: [Rd] S4 setClass / initialize misunderstanding
In-Reply-To: <6386.66.201.56.150.1265148736.squirrel@www.stat.berkeley.edu>
References: <6386.66.201.56.150.1265148736.squirrel@www.stat.berkeley.edu>
Message-ID: <4B68E74A.3010700@fhcrc.org>

On 02/02/2010 02:12 PM, bullard at stat.berkeley.edu wrote:
> 
> Hi, I recently ran into this problem. I couldn't find any mention of it in
> the setClass documentation.
> 
> setClass("Foo", representation(file = "character"))
> setMethod("initialize", "Foo", function(.Object, file) {
>   print(file)
> })
> setClass("Bar", contains = "Foo")
> 
> And the error:
> 
> Error in print(file) : argument "file" is missing, with no default
>
> The workaround is to interchange the setMethod and the second setClass
> call, however, it begs the question why is setClass calling an initialize
> method? As always, if I have missed documentation concerning this please
> point me there.

Hi Jim -- The hint is in traceback()

> setClass("Bar", contains="Foo")
Error in print(file) : argument "file" is missing, with no default
> traceback()
[snip]
5: new(toDef)
4: .simpleCoerceExpr(Class, to, names(slots), classDef2)
3: makeExtends(name, what, slots = slots, classDef2 = whatClassDef,
       package = package)
2: makeClassRepresentation(Class, properties, superClasses, prototype,
       package, validity, access, version, sealed, where = where)
1: setClass("Bar", contains = "Foo")

the source

 if(!isVirtualClass(toDef))
    toClass <- class(new(toDef)) # get it with the package slot correct

subversion

% svn annotate src/library/methods/R/RClassUtils.R

 48221        jmc     if(!isVirtualClass(toDef))
 48221        jmc         toClass <- class(new(toDef)) # get it with

% svn log -r 48221
------------------------------------------------------------------------
r48221 | jmc | 2009-03-26 14:28:03 -0700 (Thu, 26 Mar 2009) | 1 line

fix bug in defining as() methods--got wrong package for simple contains
class with no added slots
------------------------------------------------------------------------

and the mailing list

https://stat.ethz.ch/pipermail/r-devel/2009-March/052829.html

but the bottom line is that you want

  validObject(new("Foo"))

to return TRUE. I'd write (if an initialize method is really required --
what happens if there's a constructor Foo() that constructs the
arguments to new("Foo", <...>) correctlyl?)

  setMethod(initialize, function(.Object, ..., file=character(0)) {
      ## process file, then
      callNextMethod(.Object, ..., file=file)
  })

'file' gets a default argument. '...' allows derived classes to
callNextMethod (explicitly, or implicitly by a call to new("Bar",
<...>)) and get the expected 'initialize' behavior. Placing 'file' after
'...' means that 'file' doesn't capture unnamed arguments, which are
supposed to be prototypes for classes that .Object extends.

Martin


> 
> Thanks, jim
> 
> ______________________________________________
> 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] S4 setClass / initialize misunderstanding
Martin Morgan