On 11/1/10 12:28 PM, Douglas Bates 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 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.
So, the idea is that
object$predictor <- newPredictor
would, as a side effect, alter various other fields in the (now
refererence class) object?
Certainly that could be done by defining an accessor function for
predictor (but then predictor can't itself be a data field, you would
need some other name to store the data in the predictor--see below).
My taste would be for defining a "setter" method, setPredictor(x),
because that hints to the user that more is going on here than just
modifying one field.
Accessor functions work fine, but they are really intended for the case
that a field is actually stored somewhere else. Rcpp uses them for
fields that are really in C++ objects (in fact, Romain's use of them
inspired the general idea; I just stole his notion and moved it over to
reference classes).
Two points of detail:
- they are just uses of the active binding facility in R for
environments (which may be less than helpful if you aren't familiar with
those).
- the natural error is to think that you can have both the field as a
data object and the field as an accessor function with the same name.
Since they _are_ active bindings that will generate an infinite loop, as
I pointed out re Jon Clayden's example in this thread,
https://stat.ethz.ch/pipermail/r-devel/2010-October/058902.html
(Again, the Rcpp use is typical.)
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
<rom...@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<ba...@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@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@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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel