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

Dirk Eddelbuettel edd at debian.org
Mon Sep 21 17:36:35 CEST 2015


peter dalgaard <pdalgd <at> 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.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



More information about the R-help mailing list