Charles Danko
2007-Nov-16 20:49 UTC
[Rd] Returning vectors of two different data types back to R environment (from C).
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. Thanks very much for your help! Charles [[alternative HTML version deleted]]
Roger Bivand
2007-Nov-16 21:00 UTC
[Rd] Returning vectors of two different data types back to R environment (from C).
On Fri, 16 Nov 2007, 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.Put them in a list? The SEXP function in the .Call() interface then returns a list wich you unpack on the R side. There are plenty of examples in the base code and in numerous contributed packages.> > Thanks very much for your help! > Charles > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
Barry Rowlingson
2007-Nov-16 21:07 UTC
[Rd] Returning vectors of two different data types back to R environment (from C).
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.How does any R function return two different-typed values back to its caller? With a list - like list(foo=1,bar='baz'). Construct an R list in your C code and return that... R Extensions should tell you how! Barry
statmobile at gmail.com
2007-Nov-17 00:32 UTC
[Rd] Returning vectors of two different data types back to R environment (from C).
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 function: 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: http://rgonzui.nakama.ne.jp/ Personally, I found the source code for det() to be a useful example, you can search for that on R gonzui. Good Luck
Oleg Sklyar
2007-Nov-17 00:53 UTC
[Rd] Returning vectors of two different data types back to R environment (from C).
SEXP x,y,res; PROTECT(res = allocVector(VECSXP, 2)); SET_VECTOR_ELT(res, 0, x = allocVector(REALSXP, 20)); SET_VECTOR_ELT(res, 1, y = allocVector(INTSXP, 10)); // code with x,y goes here UNPROTECT(1); return res; On Fri, 2007-11-16 at 15:49 -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. > > Thanks very much for your help! > Charles > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel