Re: [R] how to add 1 + 1 with the interface between R and C

2015-09-21 Thread Cleber Borges

I think is time to learning Rcpp! :-)

thank you   (Peter Dalgaard and Dirk Eddelbuettel )  by the examples!

the more simples are often more informatives...

cleber

Em 21/09/2015 12:36, Dirk Eddelbuettel escreveu:

peter dalgaard  gmail.com> writes:

C is call by value and k and res are pointers. You need a dereferencing

step 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::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

__



---
Este email foi escaneado pelo Avast antivĂ­rus.
https://www.avast.com/antivirus

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to add 1 + 1 with the interface between R and C

2015-09-21 Thread Dirk Eddelbuettel
peter dalgaard  gmail.com> writes:
> C is call by value and k and res are pointers. You need a dereferencing 
step 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::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

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to add 1 + 1 with the interface between R and C

2015-09-21 Thread peter dalgaard

> On 21 Sep 2015, at 04:03 , Cleber N.Borges  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@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to add 1 + 1 with the interface between R and C

2015-09-21 Thread Cleber N.Borges

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 
#include 
#include 
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 
+ #include 
+ #include 
+ 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

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.