How to generate an R warning safely in Rcpp

后端 未结 2 1406
一个人的身影
一个人的身影 2021-02-07 04:25

We know that calling Rf_error() should be avoided in Rcpp as it involves a longjmp over C++ destructors on the stack. This is why we rather throw C++ exceptions in

2条回答
  •  旧巷少年郎
    2021-02-07 05:02

    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:

    start
    end
    Error in eval(expr, envir, enclos) : (converted from warning) test
    

    I wonder if anybody has a better idea for that.

提交回复
热议问题