[R] Sending lists from R to C using .C
Mercier Eloi
emercier at chibi.ubc.ca
Mon Apr 16 22:21:00 CEST 2012
Hi Chris,
I personally found the documentation rather...indigestible. At least 3
years ago when I developed my R package. It seems they have nicely
improved this part of the documentation though.
I'm not an expert at all, but at least you will have the point of view
of a beginner.
First, in your C or C++ file, redefine the type of you main function to
SEXP so it returns an R-type object. Then change your object(s) to SEXP
to be compatible with R. Don't forget to protect your variable(s) (and
unprotect it at the end). (For C++ only, encapsulate your function with
'extern "C"'). Also add this three headers in your file :
#include <R.h>
#include <Rinternals.h>
#include <Rdefines.h>
Run R CMD SHLIB on your C/C++ file. You should obtain a .so file.
In R, use dyn.load to load the .so file. Then you can use .Call.
////////////////////////////////////////////////////////////////////////////
Here is an example :
testC.c :
#include <R.h>
#include <Rinternals.h>
#include <Rdefines.h>
//extern "C" { // for C++ only
SEXP myCfunction (SEXP myList) //this function return a SEXP object
{
SEXP myResults; //definition of a new SEXP object, usable by R
double tmp = 0;
PROTECT(myResults = allocVector(REALSXP, 1)); //don't forget to
protect your variable !
for (int i=0; i<GET_LENGTH(myList); i++)
{
tmp = tmp + NUMERIC_VALUE(VECTOR_ELT(myList,i)); // refer
to Rinternals.h for a complet list of types
}
REAL(myResults)[0] = tmp;
UNPROTECT(1); //free the variable
return(myResults);
}
//} // end of extern "C"
Then run R CMD SHLIB testC.c
In R :
dyn.load("testC.so")
myList=list(1,2,3,4,5,6)
myResultsFromC <- .Call("myCfunction", myList=myList)
myResultsFromC
[1] 21
I hope this help.
Eloi
On 12-04-15 12:11 AM, Prof Brian Ripley wrote:
> On 15/04/2012 05:22, chris90nz wrote:
>> Hi,
>>
>> I am currently stuck on the following problem: I have a matrix where
>> each
>> element is a list of numbers of differing lengths. I am wondering
>> what would
>> be the best way to send this to my C function using .C?
>
> Follow the documentation ....
>
> This is not the list for discussing compiled code (see the posting
> guide), nor have you told us what you tried. But in fact there is
> only one way to do this, and ?.C and 'Writing R Extensions' explain it
> to you.
>
> I have no idea why you would have 'a list of numbers of differing
> lengths'. First, what is the length of a number? Second, why not
> store numbers in numeric vectors? An example (as required by the
> posting guide) would have helped compensate for an imprecise description.
>
>>
>> Cheers,
>>
>> Chris
>
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
--
Eloi Mercier
Bioinformatics PhD Student, UBC
Paul Pavlidis Lab
2185 East Mall
University of British Columbia
Vancouver BC V6T1Z4
More information about the R-help
mailing list