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

Kyle Baron kyleb at metrumrg.com
Tue Mar 6 00:04:44 CET 2018


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)
```




On Mon, Mar 5, 2018 at 4:35 PM, Michael Weylandt <michael.weylandt at gmail.com
> wrote:

> 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
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>



-- 
Kyle Baron, PharmD, PhD
Senior Scientist
Metrum Research Group
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20180305/fdad2e4b/attachment.html>


More information about the Rcpp-devel mailing list