[Rd] Calling R functions with multiple arguments from C

From: Rossouw, Ruan (RF) <Ruan.Rossouw_at_sasol.com>
Date: Thu, 24 Apr 2008 12:35:34 +0200


Hi,
I'm writing a C function that has to call a R function with multiple arguments. I've read the relevant section in Writing R Extensions, and searched the R site, mailing lists as well as rseek. I managed to call the function, but I'm now having trouble creating the argument list. I tried to create a pairlist of the arguments and send it to the R function. The R function however only recieves one argument as a list. I can change the R function to use this argument, but would like to know what I'm doing wrong. I copied the C and R code below. It is code submitted by Timur Elzhov on the the R mailing list under the discussion Calling R function from within C code that I've modified. I've read that discussion, but still got stuck. Could anybody point me in the right direction?

SEXP foo(SEXP fn, SEXP elmt1, SEXP elmt2, SEXP rho) {

    SEXP R_fcall, args, ans,s;
    PROTECT( args = s = allocList(2));
    PROTECT( R_fcall = lang2(fn, R_NilValue) );     SETCAR(s,elmt1);
    SET_TAG(s,install("x"));
    s = CDR(s);
    SETCAR(s,elmt2);

    SET_TAG(s,install("y"));
    SETCADR( R_fcall, args); 
    PROTECT( ans = eval(R_fcall, rho) ); 
    UNPROTECT(3);
    return ans;

}

foo.func <- function(x,y)match.call()

>.Call("foo",foo.func,x=10,y=12,new.env())

function(x,y)match.call()
(x = list(x = 10, y = 12))

> sessionInfo()
R version 2.7.0 (2008-04-22)
i386-pc-mingw32

locale:
LC_COLLATE=English_South Africa.1252;LC_CTYPE=English_South Africa.1252;LC_MONETARY=English_South
Africa.1252;LC_NUMERIC=C;LC_TIME=English_South Africa.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

Regards,

Ruan Rossouw                                                          
Sasol Technology R&D                                              
Reaction Technology and Industrial Statistics
Tel: +27 16 960 4717                                               
Fax: +27 11 522 4465
Cell: +27 83 724 4163                                                  
e-mail: Ruan.Rossouw_at_Sasol.Com                                        

[All views expressed are my own and not necessarily that of my employer.]



NOTICE: Please note that this eMail, and the contents thereof, is subject to the standard Sasol eMail legal notice which may be found at: http://www.sasol.com/legalnotices

If you cannot access the legal notice through the URL attached and you wish to receive a copy thereof please send an eMail to legalnotice_at_sasol.com


        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 24 Apr 2008 - 11:42:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Apr 2008 - 17:31:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive