From ripley at stats.ox.ac.uk  Tue Jul 18 14:37:05 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 18 Jul 2006 13:37:05 +0100 (BST)
Subject: [Rd] send a list from R to C
In-Reply-To: <5377049.post@talk.nabble.com>
References: <5377049.post@talk.nabble.com>
Message-ID: <Pine.LNX.4.64.0607181330280.17158@gannet.stats.ox.ac.uk>

On Tue, 18 Jul 2006, J?rn 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 at 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


[Rd] send a list from R to C
Prof Brian Ripley