Skip to content

extend validObject to validate any object (shallow and deep validation)

1 message · Hervé Pagès

#
Hi,

It would be nice if there was a tool for checking the validity of any
object. validObject() is of course the natural candidate for this but
right now it doesn't work on S3 objects:

   df <- data.frame(aa=1:4, bb=letters[4:1])
   attributes(df)$row.names <- c("A", "B", "C", "A")

'df' is an invalid data frame instance:

   > df
   Error in data.frame(aa = c("1", "2", "3", "4"), bb = c("d", "c", "b",  :
     duplicate row.names: A

However:

   > validObject(df)
   [1] TRUE
   > validObject(df, complete=TRUE)
   [1] TRUE

Also, here is another (typical) situation where it would be nice to be
able to (recursively) check the validity of the object:

   setClass("Collection", representation(things="list"))
   mycollection <- new("Collection", things=list(object1, object2, object3))

The problem is that 'validObject(mycollection, complete=TRUE)'
will return TRUE, even if one of the 3 objects stored in 'mycollection'
is invalid. I could implement my own validity method for Collection
objects but that's not a satisfactory solution because it would always
do a deep check (validity methods don't handle the 'complete' argument).

Would it make sense to modify validObject() so that, when called with
'complete=TRUE', it recursively validates the components of a list or
environment?

Thanks,
H.