Calling C code from R...wrapping C structures

View: New views
4 Messages — Rating Filter:   Alert me  

Calling C code from R...wrapping C structures

by Nathan Harmston-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everyone,

I am currently trying to call some C code from R, specifically calling a
function which populates a C struct.

typedef struct{
   // contents
} Model;

void test(Model *m){
   // fill the struct with crap
}

I compile the C code into a shared library, which loads into R properly. My
simple test functions work (i.e adding numbers etc)
setModel <- function(){
   model<-vector("list", 6)
   name(model) <- c( SET THE NAMES OF THE MODEL HERE )
   model
}
t <- setModel()
testcode <- function(setModel){


dyn.load("Simulation.so")

foo <- .C("test", Model=setModel)
foo
}

testcode(t)

However I get segfaults whenever I try to access any of the variables
contained in Model. So my question is: am I doing something wrong here? Is
list the right data type to wrap a structure in? I cant see any
documentation about wrapping structs in R.

Also when I pass strings to C from R they dont seem to be passed at all? Is
this a problem with R or a problem with me and if so how do I fix it?



Many Thanks in advance,

Nathan

        [[alternative HTML version deleted]]

______________________________________________
R-help@... 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.

Re: Calling C code from R...wrapping C structures

by Duncan Murdoch-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 5/13/2008 7:51 AM, Nathan Harmston wrote:

> Hi everyone,
>
> I am currently trying to call some C code from R, specifically calling a
> function which populates a C struct.
>
> typedef struct{
>    // contents
> } Model;
>
> void test(Model *m){
>    // fill the struct with crap
> }
>
> I compile the C code into a shared library, which loads into R properly. My
> simple test functions work (i.e adding numbers etc)
> setModel <- function(){
>    model<-vector("list", 6)
>    name(model) <- c( SET THE NAMES OF THE MODEL HERE )
>    model
> }
> t <- setModel()
> testcode <- function(setModel){
>
>
> dyn.load("Simulation.so")
>
> foo <- .C("test", Model=setModel)
> foo
> }
>
> testcode(t)
>
> However I get segfaults whenever I try to access any of the variables
> contained in Model. So my question is: am I doing something wrong here? Is
> list the right data type to wrap a structure in? I cant see any
> documentation about wrapping structs in R.
>
> Also when I pass strings to C from R they dont seem to be passed at all? Is
> this a problem with R or a problem with me and if so how do I fix it?

It is a problem in your C code.  The .C interface doesn't know about the
C declaration of the Model structure; it can only pass things it knows
about.  So your C function test() should have its header set to accept
what gets passed to it.  In your case, that's a list, so your C function
receives an array of pointers to SEXP structures; not the easiest things
to work with.  Most people using .C() pass in simple vectors (numeric,
integer, etc.), and use the .Call() interface if they really need R
internals.

Duncan Murdoch


>
> Many Thanks in advance,
>
> Nathan
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@... 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.

______________________________________________
R-help@... 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.

Re: Calling C code from R...wrapping C structures

by Ramon Diaz-Uriarte-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear Nathan,

How is you C function "test" defined? What type of argument is it
expecting? The mappings between R and C type of arguments you can pass
is explained in:

http://cran.r-project.org/doc/manuals/R-exts.html#Interface-functions-_002eC-and-_002eFortran

which also provides some examples. If you want to pass a list, you'll
probably have to use .Call, not .C.

HTH,

R.



On Tue, May 13, 2008 at 1:51 PM, Nathan Harmston
<iwanttobeabadger@...> wrote:

> Hi everyone,
>
>  I am currently trying to call some C code from R, specifically calling a
>  function which populates a C struct.
>
>  typedef struct{
>    // contents
>  } Model;
>
>  void test(Model *m){
>    // fill the struct with crap
>  }
>
>  I compile the C code into a shared library, which loads into R properly. My
>  simple test functions work (i.e adding numbers etc)
>  setModel <- function(){
>    model<-vector("list", 6)
>    name(model) <- c( SET THE NAMES OF THE MODEL HERE )
>    model
>  }
>  t <- setModel()
>  testcode <- function(setModel){
>
>
>  dyn.load("Simulation.so")
>
>  foo <- .C("test", Model=setModel)
>  foo
>  }
>
>  testcode(t)
>
>  However I get segfaults whenever I try to access any of the variables
>  contained in Model. So my question is: am I doing something wrong here? Is
>  list the right data type to wrap a structure in? I cant see any
>  documentation about wrapping structs in R.
>
>  Also when I pass strings to C from R they dont seem to be passed at all? Is
>  this a problem with R or a problem with me and if so how do I fix it?
>
>
>
>  Many Thanks in advance,
>
>  Nathan
>
>         [[alternative HTML version deleted]]
>
>  ______________________________________________
>  R-help@... 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.
>



--
Ramon Diaz-Uriarte
Statistical Computing Team
Structural Biology and Biocomputing Programme
Spanish National Cancer Centre (CNIO)
http://ligarto.org/rdiaz

______________________________________________
R-help@... 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.

Re: Calling C code from R...wrapping C structures

by Nathan Harmston-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

My function test is properly declared in the header file of the code:

void test(Model *);

and we just discovered yesterday we should be using .Call instead ,of .C.
However we still can't get it to work. We arent trying to pass a primitive
type from R but a complex one. We are trying to interface R with a C shared
library, which is already written. Can structs be defined in R and passed to
C and returned (by reference obviously). We don't really want to pass a list
rather a struct, so would a frame be a better idea?

This seems like it should be possible in R...(sorry I dont have much
experience in linking C and R together)

Nathan

2008/5/14 Ramon Diaz-Uriarte <rdiaz02@...>:

> Dear Nathan,
>
> How is you C function "test" defined? What type of argument is it
> expecting? The mappings between R and C type of arguments you can pass
> is explained in:
>
>
> http://cran.r-project.org/doc/manuals/R-exts.html#Interface-functions-_002eC-and-_002eFortran
>
> which also provides some examples. If you want to pass a list, you'll
> probably have to use .Call, not .C.
>
> HTH,
>
> R.
>
>
>
> On Tue, May 13, 2008 at 1:51 PM, Nathan Harmston
> <iwanttobeabadger@...> wrote:
> > Hi everyone,
> >
> >  I am currently trying to call some C code from R, specifically calling
> a
> >  function which populates a C struct.
> >
> >  typedef struct{
> >    // contents
> >  } Model;
> >
> >  void test(Model *m){
> >    // fill the struct with crap
> >  }
> >
> >  I compile the C code into a shared library, which loads into R
> properly. My
> >  simple test functions work (i.e adding numbers etc)
> >  setModel <- function(){
> >    model<-vector("list", 6)
> >    name(model) <- c( SET THE NAMES OF THE MODEL HERE )
> >    model
> >  }
> >  t <- setModel()
> >  testcode <- function(setModel){
> >
> >
> >  dyn.load("Simulation.so")
> >
> >  foo <- .C("test", Model=setModel)
> >  foo
> >  }
> >
> >  testcode(t)
> >
> >  However I get segfaults whenever I try to access any of the variables
> >  contained in Model. So my question is: am I doing something wrong here?
> Is
> >  list the right data type to wrap a structure in? I cant see any
> >  documentation about wrapping structs in R.
> >
> >  Also when I pass strings to C from R they dont seem to be passed at
> all? Is
> >  this a problem with R or a problem with me and if so how do I fix it?
> >
> >
> >
> >  Many Thanks in advance,
> >
> >  Nathan
> >
> >         [[alternative HTML version deleted]]
> >
> >  ______________________________________________
> >  R-help@... 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.
> >
>
>
>
> --
> Ramon Diaz-Uriarte
> Statistical Computing Team
> Structural Biology and Biocomputing Programme
> Spanish National Cancer Centre (CNIO)
> http://ligarto.org/rdiaz
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@... 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.