How to generate an R warning safely in Rcpp How to generate an R warning safely in Rcpp r r

How to generate an R warning safely in Rcpp


One of the solutions I came up with involves calling the R's warning function from Rcpp:

// [[Rcpp::export]]void test() {   Test t;   Function warning("warning");   warning("test"); // here R errors are caught and transformed to C++ exceptions}

which gives the correct behavior if warn>2:

startendError in eval(expr, envir, enclos) : (converted from warning) test

I wonder if anybody has a better idea for that.


I would recommend using stop() (which is a wrapper around try/catch) instead:

With your code slightly modified:

#include <Rcpp.h>using namespace Rcpp;class Test {public:    Test() { Rcout << "start\n"; }    ~Test() { Rcout << "end\n"; }};// [[Rcpp::export]]void test() {    Test t;    Rf_warning("test");}// [[Rcpp::export]]void test2() {    Test t;    stop("test2");}/*** Roptions(warn=10)#test()test2()*/

I get the desired behaviour:

R> sourceCpp("/tmp/throw.cpp")R> options(warn=10)R> #test()R> test2()startendError in eval(expr, envir, enclos) (from srcConn#3) : test2R> 

The longjmp issue is known, but you do not win by avoiding the mechanisms we have to unwind objects.