[Rd] using "= matrix (...)" in .C calls

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Jun 17 18:39:22 CEST 2004

clayton.springer at pharma.novartis.com writes:

> Apparently the lines like:
>         dsplit =  matrix(double(1),  nsplit,3),
> Cause C arrays to be pulled over into an R matrix. However I can't figure 
> out the syntax from context nor can I find documentation.

Actually no. It *creates* an R matrix (nsplit x 3) and then passes the
block of numeric data as a 1d array of nsplit. Coming back from C this
will still be an R matrix but possibly with new values inside.
help(matrix) should tell you the details. The double(1) is really just
a silly way of writing 0.0 (it specifies a double precision vector of
length 1, and the value will default to 0); matrix() will
automagically replicate it to fill the matrix.
> I have an array which was created and exists in the "C" part of the code, 
> but I can not figure out how to pull it over to the "R" side.
> The array was ALLOCed as 1-D array (of size nodes * variables), and 
> ultimately I would like to get into matrix of nodes * variables.
> Any help or advice would be appreciated.

You cannot pull, only push, when dealing with .C (I suspect that's not
quite true but it's a close approximation). The canonical way is to
dimension the array on the R side and pass it as an argument to the C

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

More information about the R-devel mailing list