[R] type conversion

Petr Savicky savicky at cs.cas.cz
Sun Mar 4 23:26:01 CET 2012


On Sun, Mar 04, 2012 at 03:18:33PM -0600, Matyas Sustik wrote:
> Hi All,
> 
> I am confused by a type conversion happening against my intent.
> 
> In an R script I allocate a matrix X, and I pass it to a C function
> by using
> 
> tmp -< .C(..., as.double(X),...).
> 
> I use as.double() because I read that it makes sure that the
> parameter passing is correct.
> 
> I return the matrix from the R script using:
> 
> return (list(..., X = tmp$X, ...))
> 
> The returned value is not a matrix but a flat vector.
> 
> I must not understand something fundamental that is happening
> here.

Hi.

No, this is correct. See section 5.2 Interface functions .C and
.Fortran in R-exts.pdf for the list of C types. There is int * and
double *, which are vectors. Your R code should restore the dim
attribute after the return from C code, for example using matrix()
with appropriate nrow and ncol parameters.

If you want to pass really a matrix, use .Call, which is more
powerful.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list