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

Michael Weylandt michael.weylandt at gmail.com
Tue Mar 6 00:16:22 CET 2018


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.

On Mon, Mar 5, 2018 at 5:04 PM, Kyle Baron <kyleb at metrumrg.com> wrote:
>
> 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
>


More information about the Rcpp-devel mailing list