[R] S4 Reference Classes: undesired behavior when calling method '$field()'

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 20 17:53:35 CET 2013


On Wed, Mar 20, 2013 at 12:38 PM, Janko Thyson
<janko.thyson.rstuff at googlemail.com> wrote:
> Dear list,
>
> I came across a behavior that IMHO is somewhat undesired when calling
> '$field()':
> If the field name whose value you're trying to get is *not* a valid
> field of the Reference Class, then R doesn't stop there with an error,
> but scans through all enclosing environments/frames. The result is
> something similar to calling '|get(<objname>, inherits=TRUE)|' when
> you'd actually expect something like ' get(<objname>, inherits=FALSE)'
> to be the default (at least I do) in order to avoid undesired retrieval
> results.
>
> Here's a little illustration of the behavior:
> http://stackoverflow.com/questions/15529251/avoiding-consideration-of-enclosing-frames-when-retrieving-field-value-of-a-s4-r

Note that that behavior is documented in ?setRefClass :

$field(name, value)

With one argument, returns the field of the object with character
string name. With two arguments, the corresponding field is assigned
value. Assignment checks that name specifies a valid field, ** but the
single-argument version will attempt to get anything of that name from
the object's environment. **

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list