Ricardo Zorzetto Nicoliello Vencio
2004-Feb-01 14:55 UTC
[R] I can't make .C(...) web-page example.
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 ?
Change the name. There is a convolve C function in R 1.8.x (but not R-devel) that is being found rather than yours. On Sun, 1 Feb 2004, Ricardo Zorzetto Nicoliello Vencio wrote:> > 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 ? > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hi! I am by no means an R-expert nor a C-expert but the document "An Introduction to .C Interface to R" by Roger D. Peng and Jan de Leeuw helped me a lot to find out how it works in principle to use the .C function call. You can find it on the homepage of Roger D. Peng (http://www.biostat.jhsph.edu/~rpeng/ ) at the following URL: http://www.biostat.jhsph.edu/~rpeng/docs/interface.pdf Hope this is what you were looking for. Best, Roland> -----Original Message----- > From: Ricardo Zorzetto Nicoliello Vencio [SMTP:rvencio at ime.usp.br] > Sent: Sunday, February 01, 2004 3:56 PM > To: R-help at stat.math.ethz.ch > Subject: [R] I can't make .C(...) web-page example. > > > 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%20 > functions%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 ? > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html+++++ This mail has been sent through the MPI for Demographic Research. Should you receive a mail that is apparently from a MPI user without this text displayed, then the address has most likely been faked. If you are uncertain about the validity of this message, please check the mail header or ask your system administrator for assistance.
Ricardo Zorzetto Nicoliello Vencio wrote:> 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.Most easily, use the R wrapper to compile and link files as in: R CMD SHLIB convolve.c which produces an .so file you can dyn.load(). (If you are on Windows: Rcmd SHLIB makes a dll.) Uwe Ligges> > Someone knows what is happening to me ? > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Ricardo Zorzetto Nicoliello Vencio <rvencio at ime.usp.br> writes:> 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 ?Looks like you haven't quite understood how to generate a shared library (R CMD SHLIB) and load it dynamically (dyn.load), and then somehow pick up a different convolve entry point (doesn't seem possible with Linux, but you're not tellig us what your system is...). Either that or you managed to confuse R badly through some earlier attempts to load the module. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907