[Rd] S4 classes and objects -- fixed structure? No...

J ö rg Beyer Beyerj at students.uni-marburg.de
Mon Aug 28 16:26:31 CEST 2006


Gabor, Roger, 

thanks a lot for your immediate answers.
Knowing that this is subject to change means a lot less coding for me :-)
Good news ... for me, and first of all for the impressing S4 system.

Thanks again

Joerg



Am 28.08.2006 16:08 Uhr schrieb Gabor Grothendieck
(<ggrothendieck at gmail.com>):

> Under R 2.3.1 these work as you indicate but under R 2.4.0
> they all give errors:
> 
>>  setClass("track", representation(x="numeric", y="numeric"))
> [1] "track"
>>  tr <- new( "track" )
>>  tr[ "ping" ] <- "pong"
> Error in "[<-"(`*tmp*`, "ping", value = "pong") :
>         object is not subsettable
>>  tr$bingo <- "bongo"
> Error: cannot coerce type S4 to list vector
> In addition: Warning message:
> Coercing LHS to a list
>>  tr[[ "blaa" ]] <- c( 200, 300 )
> Error in "[[<-"(`*tmp*`, "blaa", value = c(200, 300)) :
>         object is not subsettable
>>  tr[ "blaa" ]
> Error in tr["blaa"] : object is not subsettable
>> R.version.string
> [1] "R version 2.4.0 Under development (unstable) (2006-08-08 r38825)"
> 
> 
> On 8/28/06, Jörg Beyer <Beyerj at students.uni-marburg.de> wrote:
>> Hello.
>> 
>> Suppose you define a new S4-class, say
>>>  setClass("track", representation(x="numeric", y="numeric"))
>> 
>> Don't worry if you have a deja vu, it's from the help page.
>> Your new class is said to have a fixed structure: two slots, x, and y,
>> and that should apply to all objects you construct as members of that class.
>>>  tr <- new( "track" )
>> 
>> Now do the following:
>>>  tr[ "ping" ] <- "pong"
>>>  tr$bingo <- "bongo"
>>>  tr[[ "blaa" ]] <- c( 200, 300 )
>> 
>> Of course you can use the well known operators to access these "list entries
>> in a S4-class object":
>>>  tr[ "blaa" ]
>>>  etc.
>> 
>> You see what can happen if you decide to do a bit stress testing. The
>> question is not whether my examples makes sense or not. The question is if
>> these examples should be possible at all.
>> I wonder which is true,
>> -- the theory (Chambers, 1998, p. 279ff; Venables and Ripley, 2000, p. 99ff:
>> "... All objects in a [S4] class must have the same structure. ..."; etc.)
>> -- or the actual implementation in R (see my example, which successfully
>> violates the design principles of the language)
>> 
>> Bug, or feature? Any clarifications are appreciated -- it may be my
>> half-cooked knowledge that I find this confusing and dangerous.
>> 
>> Best
>> 
>> Joerg Beyer
>> 
>> P.S.:
>> Oh, the specs: PowerMac G4/400 PCI -- 1GB RAM -- Mac OS X 10.4.6 -- R 2.2.1
>> 
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>




More information about the R-devel mailing list