I am trying to create an R package with C ++ without using Rcpp (I know that Rcpp works wonderfully). I read a couple of R tutorials and briefly read "Writing R-Extensions". Below example 1) works, but example 2) R fails. I want to know why this happens (Are there any preliminary steps for writing functions for R, etc. ??).
Example 1
In .cpp
file
extern "C" {
SEXP add(SEXP a, SEXP b) {
SEXP result = PROTECT(allocVector(REALSXP, 1));
REAL(result)[0] = asReal(a) + asReal(b);
UNPROTECT(1);
return result;
}
}
Call in R:
.Call("add", 3.0, 2.0).
This example works.
Example 2
In .cpp
file:
extern "C" {
void RHello() {
Rprintf("Hello.\n");
R_FlushConsole();
R_ProcessEvents();
}
}
Call in R:
.Call("RHello").
This will cause R. to crash.
source
share