[Rcpp-devel] Rcpp::stop() equivalent of base::stop(..., call.=FALSE)

Michael Weylandt michael.weylandt at gmail.com
Mon Mar 5 23:35:17 CET 2018


Hi,

Is there any (easy) way to get Rcpp::stop() to behave like
base::stop(..., call.=FALSE)? That is, to just print the error message
(possibly preceded by "Error: ") without the name of the calling
function.

Right now, the error message produced by an Rcpp::stop call returns
info about the caller, which isn't helpful for my users. (I do a bit
of trickery to build an argument list and then use do.call() to call
the RcppAttributes-generated wrapper) I'm happy to put the function
name in the error message, but I'd like to remove the unhelpful part.

Here's an (admittedly awkward) minimal example:

```{r}
library(Rcpp)

sourceCpp(code='
#include "Rcpp.h"

// [[Rcpp::export]]
Rcpp::NumericVector internal_function_name(Rcpp::NumericVector x){
    Rcpp::stop("My error message.");
    return x + 2;
}')

add2 <- function(x){
    if(!is.numeric(x)){
        x <- as.numeric(x)
    }

    do.call(internal_function_name, list(x))
}

add2(1:5)
```

This prints: "Error in (function (x)  : My error message." I'm hoping
to get "Error: My error message."

Cheers,
Michael


More information about the Rcpp-devel mailing list