From murdoch.duncan at gmail.com  Sun Apr 25 13:09:08 2010
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Sun, 25 Apr 2010 07:09:08 -0400
Subject: [Rd] S4 Inheritance of environments
In-Reply-To: <g2j5832a2d11004241015u7087d1ecye95136b7b178b236@mail.gmail.com>
References: <g2j5832a2d11004241015u7087d1ecye95136b7b178b236@mail.gmail.com>
Message-ID: <4BD422D4.7000601@gmail.com>

On 24/04/2010 1:15 PM, Christopher Brown wrote:
> I looked through the documentation and the mailing lists and could not
> find an answer to this.  My apologies if it has already been answered.
>  If it has, a pointer to the relevant discussion would be greatly
> appreciated.
>   

Environments are unusual in that they are reference objects:  if e is an 
environment, and you assign f <- e, then f refers to the same object as 
e does.  This is unusual in R, where most objects are copied on 
assignment (logically, not always physically).  It means that attributes 
on environments behave strangely:  if you put an attribute on e and 
remove the same attribute from f, it is gone from e too.  We've 
discussed removing the possibility of putting attributes on environments 
(just as you can't put attributes on NULL), but haven't done so yet.

What you should do if you want to use an environment in the way you're 
using it is to put it in a container.  For S4, that could mean using an 
environment as a slot, or inheriting from an object like list(e), rather 
than directly from e.

Duncan Murdoch
> Creating S4 classes containing environments exhibits unexpected
> behavior/features.  These have a different in two ways:
>
> 1) slotName for the data: ".xData" instead of ".Data" and do not respond to the
> 2) Response to the is.* function seems to indicate that the object
> does not know of its inheritance.  ( Notably, the inherits function
> works as expected. )
>
> Here is a working illustration:
>
>   
>> #  LIST
>> setClass( 'inheritList', contains='list')
>>     
> [1] "inheritList"
>   
>> inList <- new( 'inheritList' )
>> class( inList )
>>     
> [1] "inheritList"
> attr(,"package")
> [1] ".GlobalEnv"
>   
>> is.list( inList )          # TRUE
>>     
> [1] TRUE
>   
>> slotNames(inList)          # ".Data"
>>     
> [1] ".Data"
>   
>> inherits(inList, 'list' )  # TRUE
>>     
> [1] TRUE
>   
>> # ENVIRONMENT
>> setClass( 'inheritEnv', contains='environment' )
>>     
> Defining type "environment" as a superclass via class ".environment"
> [1] "inheritEnv"
>   
>> inEnv <- new( 'inheritEnv' )
>> class(inEnv)
>>     
> [1] "inheritEnv"
> attr(,"package")
> [1] ".GlobalEnv"
>   
>> is.environment(inEnv)             # FALSE
>>     
> [1] FALSE
>   
>> slotNames(inEnv)                  # ".xData"
>>     
> [1] ".xData"
>   
>> inherits(inEnv, 'environment' )   # TRUE
>>     
> [1] TRUE
>
> My questions is whether this behavior is a bug? By design?  A work
> around?  Etc.?
>
> Thanks kindly for your reply,
>
> Chris
>
>
> the Open Data Group
>  http://www.opendatagroup.com
>  http://blog.opendatagroup.com
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] S4 Inheritance of environments
Duncan Murdoch