[R] How can I pass a R matrix as parameter to C code?

Liaw, Andy andy_liaw at merck.com
Mon Mar 20 22:35:40 CET 2006


From: johan Faux
> 
> Hello, 
>    
>   Is it possible to pass R matrix as a parameter to an 
> internal C procedure?
>   From the documentation I got the impression that only 1-dim 
> vectors can be passed. 
>   Why the following wont work for me?

In short, because a matrix in R is just a vector with dim attribute.  .C()
will pass the pointer to a copy of that vector to the C function, and return
the result in a list.  If you want to work with pointer-to-pointer, you need
to do that yourself, starting with what R gives you.  Alternatively you can
use the .Call() interface and work with the R object directly.

Andy
    
>    
>   > a<-matrix(1:15,3,5)
> > .C("pr",as.integer(a))
>    
>   void pr(int **a){
>   a[1][1]=100;
> }
>    
>    
>   Thanks,
>   Johan
>    
> 
> 		
> ---------------------------------
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list