From maechler at stat.math.ethz.ch  Fri May 19 11:46:02 2006
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Fri, 19 May 2006 11:46:02 +0200
Subject: [Rd] S4 classes and C
In-Reply-To: <m23bf79lz7.fsf@ziti.fhcrc.org>
References: <1147974317.10544.23.camel@iron.psg.net>
	<m23bf79lz7.fsf@ziti.fhcrc.org>
Message-ID: <17517.37850.60690.443326@stat.math.ethz.ch>

>>>>> "Seth" == Seth Falcon <sfalcon at fhcrc.org>
>>>>>     on Thu, 18 May 2006 12:22:36 -0700 writes:

    Seth> Ross Boylan <ross at biostat.ucsf.edu> writes:
    >> Is there any good source of information on how S4 classes (and methods)
    >> work from C?

Hmm, yes; there's nothing in the "Writing R Extensions" manual,
and there's not so much in the ``The Green Book''
(Chambers 1998), which is prominently cited by Doug Bates (and Saikat Debroy)
in the paper given at DSC 2003 (mentioned earlier in this
thread, http://www.ci.tuwien.ac.at/Conferences/DSC-2003/Drafts/BatesDebRoy.pdf )


    >> E.g., for reading
    >> how to read a slot value

GET_SLOT(); also  SET_SLOT(), NEW_OBJECT(), MAKE_CLASS(), ...

    >> how to invoke a method

Well, an S4 method is still an R function so that's partly
covered by "R-exts" -- something I may want to avoid, rather
call the C function that *underlies* the (R) S4 method...

    >> how to test if you have an s4 object

{I'd usually do those tests in R code}

    Seth> You might look at Bioconductor's Ruuid package.  It does some of this
    Seth> and is fairly small, so it is easy to see what is going on.

    Seth> http://bioconductor.org/packages/1.8/bioc/html/Ruuid.html

Indeed,.  And the package which lead to the paper has later
become 'lme4' the C code of which *currently* is in the 'Matrix'
package  -- which is much larger than Ruuid, but I think still
contains a many interesting examples of  using S4 classes from C.
Since Matrix development uses a publicly-readable subversion
archive, you can directly browse the code at
https://svn.R-project.org/R-packages/trunk/Matrix/ (but for
extensive browsing do download and unpack the released version
of from CRAN, e.g., the US mirror,
http://cran.us.r-project.org/src/contrib/Descriptions/Matrix.html )
Particularly look at the src/...Matrix.c files and their
R/...Matrix.R counterparts.

Martin

    Seth> + seth



[Rd] S4 classes and C
Martin Maechler