[R] I can't make .C(...) web-page example.

Ricardo Zorzetto Nicoliello Vencio rvencio at ime.usp.br
Sun Feb 1 15:55:59 CET 2004


Hi everyone!

I'm trying to lear how to call external C code in R but even the R help
web-page example is drive me crazy.

I copy-paste the example at:

http://stat.ethz.ch/R-manual/R-patched/doc/manual/R-exts.html#Interface%20functions%20.C%20and%20.Fortran

here is the example:

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


but using the conv function in simple examples give me trouble:

> conv(1:10,2:11)
Error in conv(1:10, 2:11) : pairlist object cannot be coerced to double

> conv( rnorm(10,10,1) , rnorm(10,11,1) )
Segmentation fault

> conv( c(10.53, 8.456, 6.6) , c(10.53, 8.456, 6.6) )
Error in conv(c(10.53, 8.456, 6.6), c(10.53, 8.456, 6.6)) :
        cannot allocate vector of length 1717986918

> a = c(1.46756, 5.56456, 2.3646)
> b = a + 5.4
> conv(a,b)
Error in conv(a, b) : cannot coerce type promise to double vector


and so on.

These results appear if I have only the .c file or if I use gcc -c conv.c
to create .o file. I cannot create executable because there is no
main(...) in .c file but just the copy-pasted example.


Someone knows what is happening to me ?




More information about the R-help mailing list