How to generate an R warning safely in Rcpp

后端 未结 2 1403
一个人的身影
一个人的身影 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:23

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

    With your code slightly modified:

    #include 
    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");
    }
    
    /*** R
    options(warn=10)
    #test()
    test2()
    */
    

    I get the desired behaviour:

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

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

提交回复
热议问题