S4 object in R cannot be passed to Fortran
سؤال
I use the bdiag
function in the Matrix
package in R to generate diagonal matrix, and then I pass the resultant matrix (called mat
) into a self-written function but R fails to execute due to the following error:
Error: invalid mode (S4) to pass to Fortran (arg 1)
I checked isS4(mat)
and it's TRUE
. Thus, I guess there is a way to convert the S4 object somehow in order to be passed to the function. Any advice will be greatly appreciated!
UPDATE: I use the following codes for constructing the block diagonal matrix:
grp.ids <- as.factor(c(rep(1,8), rep(2,4), rep(3,2)))
x <- model.matrix(~grp.ids)
X <- do.call(bdiag, replicate(238, x, simplify=FALSE))
Is there any other way to get a S3 matrix without using the bdiag
function? Thanks!
المحلول
Only the .Call()
interface can pass full R objects down to C or C++ code, see Section 5 of the Writing R Extensions manual. With .Fortran()
and .C()
you are limited to basic vectors of int
, double
, ... and their corresponding Fortran types.