Skip to content

S4 classes, creating in C

4 messages · Laurence Kell FM CEFAS, Douglas Bates, Duncan Temple Lang +1 more

#
Laurence Kell FM CEFAS <L.T.Kell at cefas.co.uk> writes:
I'm not sure.  This may be the type of question that requires John
Chambers' attention and I think he is away from his email this week.
#
I don't believe the fact that you are doing this in C code is relevant
in this problem. 

If you define LUdecomposition as
 setClass("LUdecomposition", representation(a="matrix", pivot = "integer"),
                             prototype=list(pivot = NA)                    )


you will get an error if you simply type

  new("LUdecomposition")


To make this work,  define the class as

 setClass("LUdecomposition", representation(a="matrix", pivot = "integer"),
                             prototype=list(pivot = as.integer(NA)                    ))

Why the original does not work is because 
  typeof(NA) 
returns "logical".
So it is merely the type of the value that is the problem.

 HTH,
  Duncan
Douglas Bates wrote:

  
    
#
Hi Laurence,
Laurence Kell FM CEFAS wrote:
[...]
just tried your example in R 1.6.2 and it worked. And as you say for R 1.7.1 it 
won't.

So take a look at (R 1.7.1)

 > setClass("LUdecompositio", representation(a="matrix", pivot = "integer"), 
prototype(pivot=NA))
[1] "LUdecompositio"

 > lu<-new("LUdecompositio")
Error in makePrototypeFromClassDef(properties, ClassDef, immediate) :
         In making the prototype for class "LUdecompositio" elements of the 
prototype failed to match the corresponding slot class: pivot (class " integer ")

 > setClass("LUdecompositi", representation(a="matrix", pivot = "integer"), 
prototype(pivot=as.integer(NA))
+
+ )
[1] "LUdecompositi"

 > lu3<-new("LUdecompositi")
 > lu3
An object of class "LUdecompositi"
Slot "a":
<0 x 0 matrix>

Slot "pivot":
[1] NA

 > is(lu3 at pivot)
[1] "integer" "vector"  "numeric"

So providing an explicit cast in the prototype cures the problem.

HTH,

   Matthias