Discussion:
[Rcpp-devel] Rcpp::stop() equivalent of base::stop(..., call.=FALSE)
Michael Weylandt
2018-03-05 22:35:17 UTC
Permalink
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
Kyle Baron
2018-03-05 23:04:44 UTC
Permalink
Michael -

I modified your example to use Rcpp::exception.

I think this was implemented here:
https://github.com/RcppCore/Rcpp/pull/663/

Kyle


```{r}
library(Rcpp)

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

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

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

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

add2(1:5)
```
Post by Michael Weylandt
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.
```{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
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Kyle Baron, PharmD, PhD
Senior Scientist
Metrum Research Group
Michael Weylandt
2018-03-05 23:16:22 UTC
Permalink
Thanks Kyle.

I found that (added in PR#663) just after I sent my email, but it's
not quite as elegant as Rcpp::stop. It also doesn't work for
Rcpp::warning (which I left out of my initial email).

I think this would be a nice feature to have, so I'll work up a PR
which exposes it via Rcpp::stop and Rcpp::warning. [1]

Michael

[1] My initial attempt is at
https://github.com/michaelweylandt/Rcpp/tree/mw/suppress_call_info_in_stop_and_warning,
but it still needs a few tweaks before sending the PR.
Post by Kyle Baron
Michael -
I modified your example to use Rcpp::exception.
https://github.com/RcppCore/Rcpp/pull/663/
Kyle
```{r}
library(Rcpp)
sourceCpp(code='
#include "Rcpp.h"
// [[Rcpp::export]]
Rcpp::NumericVector internal_function_name(Rcpp::NumericVector x){
throw Rcpp::exception("My error message.", false);
return x + 2;
}')
add2 <- function(x){
if(!is.numeric(x)){
x <- as.numeric(x)
}
do.call(internal_function_name, list(x))
}
add2(1:5)
```
On Mon, Mar 5, 2018 at 4:35 PM, Michael Weylandt
Post by Michael Weylandt
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.
```{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
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Kyle Baron, PharmD, PhD
Senior Scientist
Metrum Research Group
Loading...