From mtmorgan at fhcrc.org  Mon Mar 22 18:01:26 2010
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Mon, 22 Mar 2010 10:01:26 -0700
Subject: [Rd] Some more help on S4 mechanism
In-Reply-To: <4BA76306.8010106@mines-paristech.fr>
References: <4BA76306.8010106@mines-paristech.fr>
Message-ID: <4BA7A266.30600@fhcrc.org>

On 03/22/2010 05:31 AM, Renard Didier wrote:
> Hello to the list
> 
> Some days ago, I placed a question on this maling-list and received an
> answer from Martin
> that I tried to follow. Unfortunately, I am still puzzled by the next step.
> I spent the whole Week-end in gathering information from the net.
> Some of them are almost contridtcory and I lack a good document to help me.

Sometimes it is better to stick closer to the documentation, e.g.,
'Writing R Extensions', help(package="methods"), ?Methods, ?Classes.

> So my first question would beL Is there a convenient document which tells
> a standard user, who laready developped a package with S3, on how to
> migrate
> to the S4 mechanism.
> 
> Secondly, this is my problem:
> According to Martin, now I use a NAMESPACE (as follows):
> 
> import (methods)
> exportPattern("^[^0]*$")
> exportMethods(
>        "[","[<-","$","$<-",
>        "plot","print"
> )
> exportClasses(
>        "anam","db","model","rule","vardir","vario","tokens","thresh"
> )
> useDynLib("RGeoS")
> importFrom(graphics,plot)
> 
> More over, in the R directory, I have a zzz.lib with the following
> contents (.onLoad) :
> 
> ".onLoad" <-
> function(...)
> {
> 
>        # Define the Classes #
>        All0.Classes()

Usually, classes are NOT defined in a function. Instead of

SomeFile.R:
----------
All0.Classes <- function() {
  setClass("Foo", <...>)
  <etc>
}
----------

you would simply

SomeFile.R
----------
setClass("Foo", <...>)
----------

where '-------'  are meant to delimit file content and <...> is meant to
be stuff you fill in.

> 
>        # Define the environment variables #
>        environ.load()
> }
> 
> The file All0.Classes (which will not be exported ... see exportPattern)
> contains
> all the class definitions (setClass functions)
> 
> Finally, I have some other functions, such as db.plot, which contain the
> following statement
> (at the end of the file):
> 
> setMethod("plot", signature(x="db"), function(x,y,...) db.plot(x,...))
> 
> My questions:
> - Should the setMethods be regrouped in a single file (as I read from
> the net).

Doesn't matter from a functional perspective, might make sense as a
strategy to organize large amounts of code.

> - What are the differences between "R CMD check" and "R CMD INSTALL" as
> far as the steps are involved.
> 
> As a matter of fact:
> - R CMD check finds that everything is OK
> - R CMD INSTALL gives me a set of warnings of the following type:
> 
> Warning in matchSignature(signature, fdef, where) :
>  in the method signature for function "[<-" no definition for class: "db"
> I did not use the Collate option (that Martin suggested) as I am not
> sure of its syntax and where I should add these statements.

in the DESCRIPTION file

Collate: FirstFile.R SecondFile.R ThirdFile.R

see 'Writing R Extensions' available from help.start()

> 
> The package works fine anyhow but I would like to correct these warnings.
> 
> By the way, as I belong to an institute where we use Fedora Core 8, I am
> stuck with R 2.8.0.
> 
> An ultimate problem: I develop two packages P1 and P2 where P2 requires P1.
> So I used the same architecture for both with NAMESPACE and .onLoad
> functions (in the zzz.lib files).
> 
> For P2, I mention that it depends upon P1 in the DESCRIPTION file. This
> information seems redundant with the NAMESPACE where I can add
> "import(P1)". Finally when, as a user, I load both by library() (first
> P1 then P2), I receive a message about function .onLoad being loaded twice.
> 
> How can I avoid this.

your export pattern includes .onLoad; arrange for .onLoad not to be
exported (a convention is to not export functions that start with '.').

Martin

> 
> As you can see, I encountered several problems when converting my S3
> into S4. I would appreciate if someone could give me a complete
> information and a godd reference.
> 
> Thanks in advance.
> 
> 
> 
> ______________________________________________
> 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] Some more help on S4 mechanism
Martin Morgan