I call C code via .Call("foo", <args>)
, where foo calls other C
functions, computes the result and returns it. The result is a list
of length 3 and I would like to name this list. To this end, foo
does this:
/* Construct result list from variables containing the results */
SEXP res = PROTECT(allocVector(VECSXP, 3)); /* list of length 3 */
SET_VECTOR_ELT(res, 0, ScalarReal(a)); /* numeric(1) */
SET_VECTOR_ELT(res, 1, somenumericvector); /* numeric(<some length>) */
SET_VECTOR_ELT(res, 2, ScalarInteger(i)); /* integer(1) */
/* Name components and return */
SEXP nms = PROTECT(allocVector(STRSXP, 3)); /* names as SEXP */
char *nms_ = CHAR(STRING_ELT(nms, 0)); /* pointer to names */
char *names[3] = {"result_numeric", "result_numeric_vector", "result_integer"};
for(i = 0; i < 3; i++) nms_[i] = names[i];
setAttrib(res, R_NamesSymbol, nms);
UNPROTECT(1);
return res;
Is this a/the correct way of constructing a named list of length 3?
The C function indeed returns back to R but once I assign the output to a
variable in R, I immediately get a segmentation fault. What might be wrong? I
can put 'debug statement's' (simple printf("...\n")
right before the above 'return
res;' and they are executed fine.
Is there any convenient way to debug C code called from R?
An alternative to BrodieG's answer is to use mkNamed
from Rinlinedfuns.h (which contains an example of how to use mkNamed
).
/* Construct named result list from variables containing the results */
const char *names[] = {"result_numeric", "result_numeric_vector",
"result_integer", ""}; /* note the null string */
SEXP res = PROTECT(mkNamed(VECSXP, names)); /* list of length 3 */
SET_VECTOR_ELT(res, 0, ScalarReal(a)); /* numeric(1) */
SET_VECTOR_ELT(res, 1, somenumericvector); /* numeric(<some length>) */
SET_VECTOR_ELT(res, 2, ScalarInteger(i)); /* integer(1) */
UNPROTECT(1);
return res;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With