Re: [Rd] send a list from R to C

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue 18 Jul 2006 - 12:37:05 GMT

On Tue, 18 Jul 2006, Jrn Schulz wrote:

>
> Hello at all !
>
> What is the correct form to give a list form R (e.g. list(c(-1,1),
> "TestFile") ) to C. I have no problems, if I don't use character and I have
> also no problems to give a character vector form R to C. But, if I combine
> real and strings in a list then I don't know the correct syntax.
>
> I try it with following (an example):
>
> SEXP writeFile(SEXP headerR){
> Image *SaveImage;
>
> PROTECT(headerR = AS_VECTOR(headerR));
> SaveImage->Xmin = (float) REAL(VECTOR_ELT(headerR, 0))[0];
> SaveImage->Ymin = (float) REAL(VECTOR_ELT(headerR, 0))[1];
> strcpy(SaveImage->Description, CHAR(STRING_ELT(headerR, 1)));
> }
>
> where
>
> typedef struct {
> float Xmin;
> float Ymin;
> char Description[80];
> } Image;
>
> But with this form the characters are doesn't correct (in this case
> Description). Perhaps a problem is that I protect the list with AS_VECTOR.
> Or is it not possible to comitted a list from R to C that contain numeric
> values and characters ?

You intended the second element of the list, so you need

CHAR(STRING_ELT(VECTOR_ELT(headerR, 1), 0)))

that is X[[2]][1], not X[1] (assuming X to be a character vector).

[This is all in `Writing R Extensions', with examples. The R-devel version of R would have given you an informative warning about the misuse of SET_VECTOR_ELT.]

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel

Received on Tue Jul 18 22:40:13 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 18 Jul 2006 - 16:27:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.