Julien Navarre
Julien Navarre

Reputation: 7840

Additionnal warning message returns an error instead of a warning

I have two functions, f_ that throws an error and f that throws a warning before calling f_.

f_ <- function() stop()
f <- function() {
  warning()
  f_()
}

Since I have a warning before the error, R produces "additionnal warning messages", but the message in this warning is not my f warning but the error produced in f_ called a 2nd time :

> f()
Error in f_() : 
In addition: Warning message:
In f() :
  Error in f_() :

It seems to works as expected if the error is produced in the same function or by a built_in function.

f <- function() {
  warning()
  stop()
}
> f()
Error in f() : 
In addition: Warning message:
In f() : 

Can someone helps me to understand what is happening there ? Thanks for any help. I'm running R version 3.3.2 on x86_64-w64-mingw32 using RStudio.

Upvotes: 4

Views: 218

Answers (1)

Anders Ellern Bilgrau
Anders Ellern Bilgrau

Reputation: 10233

I think this is caused by the Rstudio error inspector. When encountering an error Rstudio displays the possibility for traceback and debugging. I believe that is the source of the confusion (my own included). The "second" error is simply a feature in Rstudio which assists in debugging as seen below. Note the two buttons on the right allowing you to "show traceback" and "rerun with debug".

In Rstudio enter image description here

As you can see below, if you run R in a terminal, this "additional" error is not there.

In a terminal enter image description here

In your global options in Rstudio, under General tab, you can turn off the use of the debug error handler. You can also do this under Debug -> On Error. Rstudio will then not display the "additional" message.

Edit: Upon investigating a bit further, there is something odd going on though. Below, I tried to make the error and warning message a bit more informative with the following observations:

  • Calling f() many times in a row, it is not entirely clear to me when the error inspector appears and when it does not.
  • When the error inspector does appear, the warning message is not displayed. When the error inspector does not appear, the warning message is displayed.

I do not know anything about Rstudio's internals, but it is quite definitely the error inspector causing these minor issues.

enter image description here

Upvotes: 2

Related Questions