[Rcpp-devel] Rcpp and reference classes in R-2.12.0 and later

Dominick Samperi djsamperi at gmail.com
Mon Nov 1 23:51:16 CET 2010


On Mon, Nov 1, 2010 at 3:28 PM, Douglas Bates <bates at stat.wisc.edu> wrote:

> I should probably ask this question on R-devel as it is not directly
> an Rcpp question but the thread started here so ...
>
> I'm confused about accessor functions as described in
> help("setRefClass") under the argument "fields".  It states
>
>          The element in the list can alternatively be an _accessor
>          function_, a function of one argument that returns the field
>          if called with no argument or sets it to the value of the
>          argument otherwise. Accessor functions are used internally
>          ...
>

The last example I sent was for accessor functions (auto-generation
of getX() and setX()). To use accessor fields you can do something
like:

clgen <- setRefClass("Myclass",
    fields = list(extptr = externalptr,
      xyz = function(arg=nil) {
          .Call('accessorXyz',extptr, arg)
      })

Here extptr will have to be set in an initialize method, and
the .Call will return xyz (a field in the C++ structure
pointed to by extptr) when arg is nil, otherwise the
supplied arg will be assigned to this field.

Romain has partially wrapped this process in Module,
where you can get a similar effect with:

clgen <- Module('Myclass',PACKAGE='YourPackage')$Myclass

The times they are a-changin...

The application I have in mind is classes of objects representing the
> response and auxiliary information in linear, generalized linear,
> nonlinear, and generalized nonlinear models.  An S4 implementation is
> in the MatrixModels package but the syntax of the update methods is
> awkward because they must replace slots then return the whole object,
> which can be rather large.  What I would like to do is have an update
> method that takes a new value of the linear predictor, updates the
> mean, the residuals, possibly the weights (for GLMs) and the weighted
> residuals.  After the update, methods for other objects can query the
> values of the weighted residuals, square root of the residual weights,
> square root of the X weights, etc.
>
> I'm trying to decide if the fields that can be queried should be
> declared as fields or as accessor functions.
>
> I haven't discovered any examples that declare accessor functions in
> the fields section.  Can anyone point out such an example to me?
>
> On Mon, Nov 1, 2010 at 10:44 AM, Romain Francois
> <romain at r-enthusiasts.com> wrote:
> > Le 01/11/10 16:34, Douglas Bates a écrit :
> >>
> >> On Mon, Nov 1, 2010 at 10:26 AM, Douglas Bates<bates at stat.wisc.edu>
> >>  wrote:
> >>>
> >>> What is a good place to start reading about using the newly installed
> >>> reference classes in R to interface with Rcpp?
> >>
> >> To answer my own question, the "Rcpp Modules" vignette.
> >
> > Perhaps it will be once we update the vignette. The content dates from
> > before we used reference classes.
> >
> > For reference classes themselves, the best place to look is
> > ?ReferenceClasses
> >
> > As Dirk pointed out, my chicago slides or our google slides might give
> > nuggets of information.
> >
> > I guess it also depends what specifically you want to learn about them.
> > Exposing a class is essentially the same as before reference classes.
> >
> > Deriving a C++ class, adding R methods to a class, etc ... is a new
> feature
> > we get for free with reference classes. This is not really Rcpp specific,
> so
> > maybe ?ReferenceClasses gives enough hints.
> >
> > Romain
> >
> > --
> > Romain Francois
> > Professional R Enthusiast
> > +33(0) 6 28 91 30 30
> > http://romainfrancois.blog.free.fr
> > |- http://bit.ly/czHPM7 : Rcpp Google tech talk on youtube
> > |- http://bit.ly/9P0eF9 : Google slides
> > `- http://bit.ly/cVPjPe : Chicago R Meetup slides
> >
> >
> > _______________________________________________
> > Rcpp-devel mailing list
> > Rcpp-devel at lists.r-forge.r-project.org
> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> >
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20101101/2302e9ee/attachment.htm>


More information about the Rcpp-devel mailing list