From mpetrovich at comcast.net  Tue Jul 11 21:04:14 2006
From: mpetrovich at comcast.net (Michael Petrovich)
Date: Tue, 11 Jul 2006 15:04:14 -0400
Subject: [Rd] Converting SEXP to primitive C types
Message-ID: <45511595-A6F9-4230-A759-F5CE29612AEB@comcast.net>

I'm new to R development, so this may be a trivial question.

In C, How do you convert a SEXP value returned by an R function to a  
primitive C type (int, double, char, array, etc.)? I've tried using  
the INTEGER(x), VECTOR_ELT(x,i), and similar functions found in /src/ 
include/Rinternals.h, but doing so results in incorrect results or a  
seg-fault.

For example, I modified /tests/Embedding/Rerror.c so that it tries to  
print the value returned by the function defined in error.R:

val = Test_tryEval(e, &errorOccurred);
printf("Returned: {%d}\n", VECTOR_ELT(val, 0) );

Where error.R is contains:

foo <-
function(x=3, y=6)
{
   z <- x * y
   z
}

But the output is just "0", not the correct "18". Any ideas?

Thanks for your help!



[Rd] Converting SEXP to primitive C types
Michael Petrovich