[Rd] Returning vectors of two different data types back to R environment (from C).

statmobile at gmail.com statmobile at gmail.com
Sat Nov 17 01:32:49 CET 2007

On Fri, Nov 16, 2007 at 03:49:09PM -0500, Charles Danko wrote:
> Hello,
> Quick question.
> I have written a C function - I would like to make it return two vectors to
> the R environment - one STRSXP vector, and one INTSXP vector.  Is this
> possible/ easy to do using the API?
> I looked, but could not find the answer to this  question in the "Writing R
> Extensions" guide.

Yes, it's really quite reasonable to do, although it's not documented
in detail in the R-extensions manual.  As stated earlier, this will
only work with .Call and SEXP functions where you can handle R objects
within C.  

In Section 5.9 "Evaluating R expressions in C" it has an example using
lists in C.  Basically, lists are VECSXP objects so you can search for
that in R-exts.

Also, I would recommend looking at 5.7.6 "Handling lists" and the

SEXP getListElement(SEXP list, char *str)

You should look for that, and then grep the source files for
examples.  If you don't have grep to search the files, then I would
recommend the R gonzui site:


Personally, I found the source code for det() to be a useful example,
you can search for that on R gonzui.

Good Luck

More information about the R-devel mailing list