[R] passing lists through .C

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Dec 12 09:14:04 CET 2001


On Tue, 11 Dec 2001, Catherine Loader wrote:

> I have a list,
>
> > rb
> $t
> [1] "tree"
>
> $x
> [1] 0
>
> $cut
> [1] 0.8
>
> $l
>  [1] 0 0
>
> and pass it through .C("fn",rb) to
>
> void fn(ev)
> int **ev;
> { double cut;
>   cut = *(double *)ev[2][0];
>   printf("%8.5f\n",cut);
> }
>
> in S-4, it produces 0.8, as I want.
> But R (version 1.3.1, linux) produces a segmentation fault.
> Is it possible to access list elements in R? The manual seems
> to suggest writing .Call interfaces, which I want to avoid.

Yes, but it would be no easier than using .Call.  ?Foreign says

Lists are passed as C arrays of `SEXP' and can be declared as `void *' or
`SEXP *'.

so you do need to mess with SEXPs.  Something like (minimally tested)

#include <R.h>
#include <Rinternals.h>

void fn(SEXP *ev)
{
   SEXP cut = ev[2];
   printf("%8.5f\n", REAL(cut)[0]);
}

Note that using printf is not portable, but presumably this was just a
test.

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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