[Rd] Problem with _new_ if class "lm" in object representatio n.

Witold Eryk Wolski wolski at molgen.mpg.de
Thu Sep 30 14:35:43 CEST 2004


Hi,

But "any" rises some other problems well known from S3. One has "any" 
for "free" in S3. You don't need S4. . But I know how "if" polluted 
functions  look like in S3. They are hard to understand and  to 
maintain. Hence I am quite happy to use S4.
Type-checking is usefull if you program with data. Also if you are on 
the "C side" of the object. A further problem which I have with "any" 
is, that lm="any" in setClass tells you exactly ANYthing (and you are 
back in S3). And thats not what I like.*
Hence, I do not want to use any in this case.

I think that allowing to assing NULL to a slot will solve some problems 
which I have with S4 so far.

a) How to express that an a slot content was not assigned so far? J.C. 
suggest to use character(0), lm(1~1) etc. The problem I have with 
insterting lm(1~1) in slot mod in my class Ctest are that I have 100000 
instances of object Ctest. At initialization everyone contains an object 
lm(1~1). But please  note the following.

 > x<-lm(1~1)
 > object.size(x)
[1] 6388
 > object.size(NULL)
[1] 0

Unfortunately S4 do not have references. Hence, I can not store a 
reference to a single instance of lm(1~1) .

Exactly the same problems I have if I want to "delete" an object. For 
example you like to have a slot  which stores some data which is 
important if the object is in one state. But if the state of the object 
changes you do not need that data anymore. Hence, you would like to 
delete it to get storage.  You can only do it if you have like you 
suggest "any", but the consequence is "if" polluted code, or using 
slot(obj,"mod",check=FALSE)<- NULL.

But the documentation for slot states.
check: " You should never set this to 'FALSE' in normal
          use, since the result can create invalid objects. "


b) The Problem which I have writing intitialization functions like 
suggested by J.C  is:  Imagine you have an object with twenty slots. You 
have to write and initialize function with approximately 20 "if"'s. One 
of the contribution of S4, as mentioned above, is to help you to avoid 
writing such "if" polluted functions. But as I sometimes forget any role 
has it's exceptions. So I can live with one function looking ugly in S4 
instead of having all functions "if" polluted like in case of S3.  The 
only consequence so far is that I try to avoid to write initialize 
functions. I do it only in some rare very special cases.

The NULL exception in slot assignement will allow me object 
initialization using just "new" without providing a "if" polluted 
function initialize.

 > setClass("bar")
[1] "bar"
 > setClass("foo", representation(x="numeric", y="bar"))
[1] "foo"
 > new("foo", x=1)
Error in validObject(.Object) : Invalid "foo" object: Invalid object for 
slot "y" in class "foo": got class "NULL", should be or extend class "bar"
 >


Are there S4 inherent solutions to this questions?


Yours.
/E

*(I would rather compare "any" to the Object class then to null in java. 
It has a different function.)

Pfaff, Bernhard wrote:

>>Hi,
>>
>>Thanks a lot for pointing me to setOldClass().
>>
>>What is exactly the point against it, to allow NULL to be a 
>>special case 
>>which can be assigned to any slot? I think in java one can 
>>assing null 
>>to any slot (I do not like java but not because of that).
>>I am asking because for some objects it may be hard to define 
>>something 
>>equivalent to numeric(0). And even lm(1~1) is not that what I 
>>am looking 
>>for. I would like to have the case
>>of an unassigned slot (Just think about storage?) even to the expense 
>>checking every time if the slot is not null.
>>    
>>
>
>
>Hello Eryk,
>
>how about "ANY" and checking where suitable/needed if the passed object for
>"mod" is of class "lm" at a later stage in your code, like in:
>
>setClass("Ctest"
>	,representation(
>		test="character"
>		,bla="character"
>		,mod="ANY" 
>		)
>	)
>
>test1 <- new("Ctest",mod=lm(1~1))
>is(test1 at mod, "lm")
>
>test2 <- new("Ctest",test="bla")
>is(test2 at mod, "lm")
>
>test3 <- new("Ctest",bla="brum")
>is(test3 at mod, "lm")
>
>test4 <- new("Ctest",test="bla",bla="brum")
>is(test4 at mod, "lm")
>
>>From there on you could use stop() or warning() or whatever you want to
>return to the user as information which class mod should belong to.
>
>HTH,
>Bernhard  
>  
>



More information about the R-devel mailing list