Re: [R] Calling R from C

From: Roger Bivand <Roger.Bivand_at_nhh.no>
Date: Tue 09 May 2006 - 20:05:45 EST


On Tue, 9 May 2006, Ingmar Visser wrote:

> Dear R-helpers,
>
> I'm trying to transfer a loop from R code to C to improve performance using
> the .Call interface. However, I ran into problems before even getting
> started, and I hope (fear) I'm missing something completely obvious here.
>
> I use the following C function (passing an integer to the C function and
> printing it):
>
> #include <R.h>
> #include <Rinternals.h>
>
> SEXP doloop(SEXP nt) {
> int x=INTEGER(nt)[0];
> Rprintf("x: %d \n", x);
> SEXP ans;
> PROTECT(ans=allocVector(REALSXP,1));
> REAL(ans)[0]=0;
> Rprintf("init ans: %f \n",REAL(ans)[0]);
> UNPROTECT(1);
> return(ans);
> }
>
> Which compiles without problems into a shared library
>
> a1648:~ ivisser$ R CMD SHLIB -l
> /Users/ivisser/Documents/projectsCurrent/dmm4Project/rfromc/
> /Users/ivisser/Documents/projectsCurrent/dmm4Project/rfromc.c
> gcc-4.0 -arch ppc -I/Library/Frameworks/R.framework/Resources/include
> -I/Library/Frameworks/R.framework/Resources/include/ppc
> -I/usr/local/include -fPIC -fno-common -g -O2 -std=gnu99 -c
> /Users/ivisser/Documents/projectsCurrent/dmm4Project/rfromc.c -o
> /Users/ivisser/Documents/projectsCurrent/dmm4Project/rfromc.o
> gcc-4.0 -arch ppc -flat_namespace -bundle -undefined suppress
> -L/usr/local/lib -o
> /Users/ivisser/Documents/projectsCurrent/dmm4Project/rfromc.so
> /Users/ivisser/Documents/projectsCurrent/dmm4Project/rfromc.o
> -L/Library/Frameworks/R.framework/Resources/lib/ppc -lR
>
> And then call:
>
> >dyn.load("rfromc.so")
> > .Call("doloop",nt)

Try:

.Call("doloop", as.integer(nt))

I think nt probably is numeric but not integer; it is safer to impose the required storage mode than leave it to chance:

> nt <- 1
> storage.mode(nt)

[1] "double"
> .Call("doloop",nt)

x: 0
init ans: 0.000000
[1] 0
> .Call("doloop", as.integer(nt))

x: 1
init ans: 0.000000
[1] 0
> sessionInfo()

Version 2.3.0 (2006-04-24)
i686-pc-linux-gnu

attached base packages:

[1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets" 
[7] "base"     



>
> With the following result (basically x is an unpredictable integer):
>
> x: 1072693248
> init ans: 0.000000
> [1] 0
>
> When I replace Rprintf("x: %d \n", x); with Rprintf("x: %f \n", x); (note
> the %f) I get:
>
> > .Call("doloop",nt)
> x: 1.000000
> init ans: 0.000000
> [1] 0
>
> Apparently x has the right value but is somehow represented as a double
> instead of as integer
>
> I'm using:
> Version 2.3.0 (2006-04-24)
> powerpc-apple-darwin8.6.0
>
> attached base packages:
> [1] "methods" "stats" "graphics" "grDevices" "utils" "datasets"
> "base"
>
> Best, Ingmar
>
> ______________________________________________
> R-help@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@nhh.no

______________________________________________
R-help@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
Received on Tue May 09 20:25:32 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 09 May 2006 - 22:10:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.