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

From: Ramon Diaz-Uriarte <rdiaz02_at_gmail.com>
Date: Wed, 14 May 2008 12:14:25 +0200

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_at_googlemail.com> 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_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.
>

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

______________________________________________
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 Wed 14 May 2008 - 14:13:29 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 Wed 14 May 2008 - 20:30:45 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