From ripley at stats.ox.ac.uk  Thu Apr 24 12:59:50 2008
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu, 24 Apr 2008 11:59:50 +0100 (BST)
Subject: [Rd] Calling R functions with multiple arguments from C
In-Reply-To: <1E4A0C6895F30F4881B516DB33ADA6047AAFE1@SASDMSG06.sasol.com>
References: <1E4A0C6895F30F4881B516DB33ADA6047AAFE1@SASDMSG06.sasol.com>
Message-ID: <alpine.LFD.1.10.0804241146210.22801@gannet.stats.ox.ac.uk>

Try PrintValue(R_fcall):

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

You want a pairlist of length 1+nargs, not a pairlist of pairlists.  Here 
is one way to do it (there are many)

#include <Rinternals.h>

SEXP foo(SEXP fn, SEXP elmt1, SEXP elmt2, SEXP rho)
{
     SEXP R_fcall, args, ans,s;
     PROTECT( R_fcall = lang3(fn, elmt1, elmt2) );
     s = CDR(R_fcall);
     SET_TAG(s, install("x"));
     s = CDR(R_fcall);
     SET_TAG(s, install("y"));
     ans = eval(R_fcall, rho);
     UNPROTECT(1);
     return ans;
}


On Thu, 24 Apr 2008, Rossouw, Ruan (RF) wrote:

> 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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Calling R functions with multiple arguments from C
Prof Brian Ripley