An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20030623/67e30f56/attachment.pl
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 am using C code to create an S4 object based on Douglas Bates's example in his lecture notes on <http://www.ci.tuwien.ac.at/Conferences/DSC-2003/Tutorials/RExtensions/slide s.pdf> http://www.ci.tuwien.ac.at/Conferences/DSC-2003/Tutorials/RExtensions/slides .pdf e.g. SEXP La_DGE_dc(SEXP A) { SEXP aa = PROTECT(duplicate(A)); SEXP adims, pivot, val; int m, n, info; if (!isMatrix(aa) || !isReal(aa)) { error("A must be a double precision matrix"); } adims = GET_DIM(aa); m = INTEGER(adims)[0]; n = INTEGER(adims)[1]; pivot = PROTECT(NEW_INTEGER(m < n ? m : n)); F77_CALL(dgetrf)(&m, &n, REAL(aa), &m, INTEGER(pivot), &info); check_Lapack_error(info, "dtrtrf"); val = PROTECT(NEW_OBJECT(MAKE_CLASS("LUdecomposition"))); SET_SLOT(val, install("a"), aa); SET_SLOT(val, install("pivot"), pivot); UNPROTECT(3); return val; } LUdecomposition is an S4 class defined as setClass("LUdecomposition", representation(a="matrix", pivot = "integer")) This works in R 1.7.0 and R 1.7.1 but if I initialise any of the slots setClass("LUdecomposition", representation(a="matrix", pivot = "integer"), prototype=list(pivot = NA) ) then I get the following error message in R 1.7.1 but not R 1.7.0 Error in makePrototypeFromClassDef(properties, ClassDef, immediate) : In making the prototype for class "LUdecomposition" elements of the prototype failed to match the corresponding slot class: pivot (class " integer ") Why can I no longer use the prototype to set the default values?
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:
Laurence Kell FM CEFAS <L.T.Kell at cefas.co.uk> writes:
I am using C code to create an S4 object based on Douglas Bates's example in his lecture notes on <http://www.ci.tuwien.ac.at/Conferences/DSC-2003/Tutorials/RExtensions/slide s.pdf> http://www.ci.tuwien.ac.at/Conferences/DSC-2003/Tutorials/RExtensions/slides .pdf e.g. SEXP La_DGE_dc(SEXP A) { SEXP aa = PROTECT(duplicate(A)); SEXP adims, pivot, val; int m, n, info; if (!isMatrix(aa) || !isReal(aa)) { error("A must be a double precision matrix"); } adims = GET_DIM(aa); m = INTEGER(adims)[0]; n = INTEGER(adims)[1]; pivot = PROTECT(NEW_INTEGER(m < n ? m : n)); F77_CALL(dgetrf)(&m, &n, REAL(aa), &m, INTEGER(pivot), &info); check_Lapack_error(info, "dtrtrf"); val = PROTECT(NEW_OBJECT(MAKE_CLASS("LUdecomposition"))); SET_SLOT(val, install("a"), aa); SET_SLOT(val, install("pivot"), pivot); UNPROTECT(3); return val; } LUdecomposition is an S4 class defined as setClass("LUdecomposition", representation(a="matrix", pivot = "integer")) This works in R 1.7.0 and R 1.7.1 but if I initialise any of the slots setClass("LUdecomposition", representation(a="matrix", pivot = "integer"), prototype=list(pivot = NA) ) then I get the following error message in R 1.7.1 but not R 1.7.0 Error in makePrototypeFromClassDef(properties, ClassDef, immediate) : In making the prototype for class "LUdecomposition" elements of the prototype failed to match the corresponding slot class: pivot (class " integer ") Why can I no longer use the prototype to set the default values?
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.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
_______________________________________________________________
Duncan Temple Lang duncan at research.bell-labs.com
Bell Labs, Lucent Technologies office: (908)582-3217
700 Mountain Avenue, Room 2C-259 fax: (908)582-3340
Murray Hill, NJ 07974-2070
http://cm.bell-labs.com/stat/duncan
Hi Laurence,
Laurence Kell FM CEFAS wrote:
[...]
LUdecomposition is an S4 class defined as
setClass("LUdecomposition", representation(a="matrix", pivot = "integer"))
This works in R 1.7.0 and R 1.7.1 but if I initialise any of the slots
setClass("LUdecomposition", representation(a="matrix", pivot = "integer"),
prototype=list(pivot = NA) )
then I get the following error message in R 1.7.1 but not R 1.7.0
Error in makePrototypeFromClassDef(properties, ClassDef, immediate) :
In making the prototype for class "LUdecomposition" elements of the
prototype
failed to match the corresponding slot class: pivot (class " integer
")
Why can I no longer use the prototype to set the default values?
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
Laurence Kell CEFAS Lowestoft Laboratory Pakefield Road Lowestoft, NR33 0HT UK Tel +44 1502 52 42 57 Fax +44 1502 52 45 11 e-mail l.t.kell at cefas.co.uk [[alternative HTML version deleted]]
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Matthias Burger Bioinformatics R&D Epigenomics AG www.epigenomics.com Kleine Pr?sidentenstra?e 1 fax: +49-30-24345-555 10178 Berlin Germany phone: +49-30-24345-0