[R] converting R objects to C types in .Call

Faheem Mitha faheem at email.unc.edu
Mon Jan 24 20:13:49 CET 2005


Hi Barry,

Thanks for your reply.

On Mon, 24 Jan 2005, Barry Rowlingson wrote:

> Firstly, R is expecting an SEXP as a return value!

Ouch...

I haven't found anything that says this explicitly, but it looks like 
.Call expects a SEXP to be returned to R. At any rate, trying to use void 
instead gives a segfault.

> And secondly, your SEXP chstr is still a vector - so you need to get an 
> element from it. If there's only one element, then that'll be the zeroth 
> element. Here's my code:

> #include <R.h>
> #include <Rinternals.h>
> #include <stdio.h>
>
> SEXP testfn(SEXP chstr)
> {
>  char * charptr = CHAR(VECTOR_ELT(chstr,0));
>  printf("%s", charptr);
>  return(chstr);
> }

> - I'm just returning the same object back in the return() statement, and 
> using VECTOR_ELT(chstr,0) to get to the 0'th element.

> SO in R:
>
> > foo = .Call("testfn","fnord")
>
> should print 'fnord' and return foo as "fnord".
>
> > foo = .Call("testfn",c("bar","baz"))
>
> will print 'bar' and return foo as c("bar","baz")

Thanks for your help. That works fine.                   Faheem.




More information about the R-help mailing list