[R] calling a C function with a struct

From: John Nolan <jpnolan_at_american.edu>
Date: Fri, 06 Jun 2008 15:32:29 -0400

I am trying to call a precompiled C function that uses a struct as one of it's arguments.
I could write a wrapper function in C, but I was hoping there is some way to
pack fields into an array of type raw that could be passed directly to the function.

Here is some more detail. The C struct is simple, but has mixed types:

    struct STRUCT1 {

       long type;
       long nx;
       double *x;
       double a;
       double b;

    };
   typedef struct STRUCT1 STRUCT1_TYPE;

The C function header is

void func1( long method, STRUCT1 my_struct, double *output);

I would like to have an R list mimicking the C struct, and then use .C to call func1 with this information, e.g.

   my.struct <- list(type=3,nx=5,x=1:5,a=2.5,b=8.3)    my.func1( 3, convert2raw( my.struct ), )

where R function convert2raw would return a vector of type raw with the fields of my.struct packed into memory just like STRUCT1, and then I could call func1 with that vector of raws.

Can I write a convert2raw( ) function and then use

   my.func1 <- function( method, buf ) {

       a <- .C("func1", as.integer(method), as.raw(buf) , output=double(1) )

       return(a$output)
   }

John

...........................................................................

 John P. Nolan
 Math/Stat Department
 227 Gray Hall
 American University
 4400 Massachusetts Avenue, NW
 Washington, DC 20016-8050

 jpnolan_at_american.edu
 202.885.3140 voice
 202.885.3155 fax
 
http://academic2.american.edu/~jpnolan

...........................................................................

        [[alternative HTML version deleted]]



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. Received on Fri 06 Jun 2008 - 20:23:44 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 Fri 06 Jun 2008 - 21:30:58 GMT.

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

list of date sections of archive