Skip to content

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

3 messages · Eric Lecoutre, Wolski

#
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,

For your lm problem, you could begin to create a virtual class that accepts 
either NULL or a lm object:

 > setClassUnion("mylm", c("NULL","lm"))
 > setClass("foo", representation(x="numeric", y="mylm"))
 > new("foo",x=1,y=NULL)

Then, you have to check the content of slot y in any function that will 
manipulate objects of class "foo":


         setMethod("show","foo",function(object)
         {
         cat("\n object of class foo")
         if (!is.null(object@y)) cat(" does have a lm object in slot y!")
         })


Eric
At 14:35 30/09/2004, Witold Eryk Wolski wrote:
Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre@stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward 
Tufte
#
Hi,

Yes indeed. Thanks.

Yours
Eric Lecoutre wrote: