/* Tests the R error handling in an application which embeds R. The application provides its own jump_now() routine which is called when cleaning up after an R error. Here, we generate the error by calling stop(). */ #include "R.h" #include "Rinternals.h" #include "Rdefines.h" /* Need to include non-public Defn.h to get the SA_NOSAVE constant for calling R_CleanUp. This is not part of the published API! It is here only to illustrate a different approach, not a recommended one. */ #include "../src/include/Defn.h" #include "embeddedRCall.h" /* */ int jump_now(void) { fprintf(stderr, "In the local error handler\n"); exit(1); } int main(int argc, char *argv[]) { SEXP fun, e, arg; int i; char *localArgs[] = {"R", "--gui=none", "--silent"}; init_R(sizeof(localArgs)/sizeof(localArgs[0]), localArgs); /* Evaluates the two expressions: source("code.R") foo() where foo is defined in the file code.R */ PROTECT(fun = Rf_findFun(Rf_install("source"), R_GlobalEnv)); PROTECT(e = allocVector(LANGSXP, 2)); PROTECT(arg = NEW_CHARACTER(1)); SET_STRING_ELT(arg, 0, COPY_TO_USER_STRING("code.R")); SETCAR(e, fun); SETCAR(CDR(e), arg); eval(e, R_GlobalEnv); UNPROTECT(2); fun = Rf_findFun(Rf_install("foo"), R_GlobalEnv); PROTECT(fun); PROTECT(arg = NEW_INTEGER(10)); e = allocVector(LANGSXP, 1); PROTECT(e); SETCAR(e, fun); eval(e, R_GlobalEnv); UNPROTECT(2); R_CleanUp(SA_NOSAVE, 0, FALSE); return(0); }