Search code examples
rcppr-package

stop in Rcpp function (as a part of a package) ends in debug mode


I am testing the use of stop in a package using Rcpp and I see that when stop is used, the function exits into debug mode in R. For example, I am using the example function at the this link (see takeLog3 function, also pasted below)

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
double takeLog3(double val) {
    if (val <= 0.0) {           // log() not defined here
        stop("Inadmissible value");
    }
    return log(val);
}

The output of takeLog3(-10) is

> takeLog3(-10)
Error in takeLog3(-10) : Inadmissible value
Called from: takeLog3(-10)
Browse[1]> 

and then I have to enter Q to exit out of debug mode. My question is, is that expected behavior? Secondly, how can I make stop statement to not go into debug mode, but quit the function completely after displaying the stop message?

Note that I see this behavior when takeLog3 is included in a package. I don't see this behavior when I have this function outside of a package in a simple .cpp file, where the function simply shows the stop message and control returns back to the console but does NOT go into debug mode.

Any help would be very much appreciated!

PS: I just found that I get the behavior I need by using Rf_error instead of stop. Can anyone provide an informed opinion, as to which one is preferred? Thank you.


Solution

  • Second question: stop() calls Rf_error() to implement the behaviour you see.

    First question: You end up in the debugger because the IDE (which you didn't name or mention) wants to be helpful. Try it on the command-line in a standard R shell:

    R> Rcpp::cppFunction("bool myStop(std::string reason) { Rcpp::stop(reason); }")
    R> myStop("tired")
    Error in myStop("tired") : tired
    R> 
    

    Ubuntu 18.10, R 3.6.0.