[R] .C help

Roger Bivand Roger.Bivand at nhh.no
Fri Sep 30 21:33:56 CEST 2005


On Fri, 30 Sep 2005, Elizabeth Lawson wrote:

> Hi,
>  I am hoping some one can help me.  I am learning to use C and would
> like to learn how to call c code in R.  I have look at Writing R
> Extensions
> 
>  
> 
>  
> 
> and I tried to copy the example on page 38 
> 
>  
> 
> void convolve(double *a, int *na, double *b, int *nb, double *ab)
> 
> {
> 
> int i, j, nab = *na + *nb - 1;
> 
> for(i = 0; i < nab; i++)
> 
> ab[i] = 0.0;
> 
> for(i = 0; i < *na; i++)
> 
> for(j = 0; j < *nb; j++)
> 
> ab[i + j] += a[i] * b[j];
> 
> }
> 
> called from R by
> 
> conv <- function(a, b)
> 
> .C("convolve",
> 
> as.double(a),
> 
> as.integer(length(a)),
> 
> as.double(b),
> 
> as.integer(length(b)),
> 
> ab = double(length(a) + length(b) - 1))$ab
> 
>  
> 
>  
> 
> and I got the error "C" function name not in load table.
> 
>  
> 
> Do I need to compile the C code first?
> 
> Do I need a c copmiler at all?
> 

Yes, you need to compile the C code first, into a file in the correct 
format to dyn.load() into R. The format varies with operating system, and 
when the appropriate compilers and other software components are present, 
can be done convieniently by entering R CMD SHLIB convolve.c at the shell 
command prompt for your system. On Linux systems, the software needed is 
usually installed already, on Windows visit 

http://www.murdoch-sutherland.com/Rtools/

for advice on where to get and how to install the C compiler used to build 
R itself. 

Do look at documentation about C, having a compiler at hand to try out 
examples and exercises is very helpful.

>  
> 
> Any suggestions for a begginner?
> 
>  
> 
> Thanks,
> 
>  
> 
> Elizabeth Lawson
> 
> 
> 
> 		
> ---------------------------------
> 
>  Click here to donate to the Hurricane Katrina relief effort. 
> 	[[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
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list