Cleber N.Borges
2015-Sep-21 02:03 UTC
[R] how to add 1 + 1 with the interface between R and C
Dear useRs, I would like some help on how to make the sum of 1 + 1 but using the interface between A and C. the function call does .call lock and close the R. Thank you for any help it. Cleber ####################### ############ R code text_code <-" #include <stdlib.h> #include <R.h> #include <Rdefines.h> void testfun( double *k, double *res ); void testfun( double *k, double *res ) { res = k + 1; } " sink('test.c') cat( text_code ) sink() system("R CMD SHLIB test.c") dyn.load( "test.dll" ) is.loaded( 'testfun' ) k=1 .C('testfun', k=as.double(k), res=as.double(0) ) #################################### ### Output > text_code <-" + #include <stdlib.h> + #include <R.h> + #include <Rdefines.h> + void testfun( double *k, double *res ); + void testfun( double *k, double *res ) + { + res = k + 1; + } + " > sink('test.c') > cat( text_code ) > sink() > system("R CMD SHLIB test.c") cygwin warning: MS-DOS style path detected: C:/R/etc/x64/Makeconf Preferred POSIX equivalent is: /cygdrive/c/R/etc/x64/Makeconf CYGWIN environment variable option "nodosfilewarning" turns off this warning. Consult the user's guide for more details about POSIX paths: http://cygwin.com/cygwin-ug-net/using.html#using-pathnames gcc -m64 -I"C:/R/include" -DNDEBUG -I"d:/RCompile/r-compiling/local/local320/include" -O2 -Wall -std=gnu99 -mtune=core2 -c test.c -o test.o gcc -m64 -shared -s -static-libgcc -o test.dll tmp.def test.o -Ld:/RCompile/r-compiling/local/local320/lib/x64 -Ld:/RCompile/r-compiling/local/local320/lib -LC:/R/bin/x64 -lR > > dyn.load( "test.dll" ) > > is.loaded( 'testfun' ) [1] TRUE > k=1 > .C('testfun', k=as.double(k), res=as.double(0) ) $k [1] 1 $res [1] 0 --- Este email foi escaneado pelo Avast antiv?rus. https://www.avast.com/antivirus
peter dalgaard
2015-Sep-21 14:33 UTC
[R] how to add 1 + 1 with the interface between R and C
> On 21 Sep 2015, at 04:03 , Cleber N.Borges <klebyn at yahoo.com.br> wrote: > > Dear useRs, > > I would like some help on how to make the sum of 1 + 1 > but using the interface between A and C. > > the function call does .call lock and close the R. > > Thank you for any help it.Either you need a basic course in C, or you need a refresher....:> > void testfun( double *k, double *res ) > { > res = k + 1; > }C is call by value and k and res are pointers. You need a dereferencing step or nothing with happen. Try *res = *k + 1; -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Dirk Eddelbuettel
2015-Sep-21 15:36 UTC
[R] how to add 1 + 1 with the interface between R and C
peter dalgaard <pdalgd <at> gmail.com> writes:> C is call by value and k and res are pointers. You need a dereferencingstep or nothing with happen. Try> > *res = *k + 1;Or you use Rcpp which writes the glue code. Save the following into a file: #include <Rcpp.h> // [[Rcpp::export]] int adder(int x, int y) { return x + y; } /*** R adder(40, 2) */ Running this is as simple as sourcing it: R> Rcpp::sourceCpp("/tmp/adder.cpp") R> adder(40, 2) [1] 42 R> and it even runs the R snippet at the bottom. Dirk