[R] Passing an R matrix to a C program

Agustin Lobo alobo at ija.csic.es
Thu Sep 27 13:55:19 CEST 2001


I'm starting to write a C function that gets
a list and a matrix from R. I'm using .Call
because I've undertood that this is recommended
to handle lists (am I wrong?).  My problem is that I
can pass the matrix as a vector, but not as a 2D array.

For example, in the following simple C program:

#include <R.h>
#include <Rdefines.h>

SEXP printListElement(SEXP list, SEXP stat)
SEXP elmt = R_NilValue, names = getAttrib(list, R_NamesSymbol);
int i; double g1, g2, g3, s1, s2, s3;
for (i = 0; i < length(list); i++) {	
	elmt = VECTOR_ELT(list, i);
	g1 = REAL(VECTOR_ELT(list, i))[0];
	s1 = REAL(stat)[12];
	printf("%ld %f %f \n", i, g1, s1);	
return elmt;

I can get the 13th element of stat, but what should
I do to write something like:
s1 = REAL(stat)[1][5]


I've tried SEXP *stat, SEXP stat[] etc.
I can always calculate the one-dimensional index
from the line and column indices, but referencing
with 2 indices would be more simple.

(The C program is called from the R function:
> testR
function (lista,stat)
        a <- .Call("printListElement",lista,stat)


Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list