R with C ++ (without Rcpp)

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 .cppfile

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 .cppfile:

extern "C" {

  void RHello() {

    Rprintf("Hello.\n");

    R_FlushConsole();
    R_ProcessEvents();

  }

}

Call in R:

.Call("RHello"). 

This will cause R. to crash.

+4
source share
1 answer

The function signature is incorrect in the second case (void return value), it should return a SEXP object. Even if it is only R_NilValue.

I hope you have a good reason not to use Rcpp.

+1
source

Source: https://habr.com/ru/post/1685834/


All Articles