5

Prior to R 4.0.1, functions and S4 generics handle errors identically:

> nrow(stop())
Error in nrow(stop()) : 
> setGeneric("nrow")
[1] "nrow"
> nrow(stop())
Error in nrow(stop()) : 

In R 4.0.1 I now get:

# base::nrow
> nrow(stop())
Error in nrow(stop()) : 
# Convert nrow to S4 generic
> setGeneric("nrow")
[1] "nrow"
> nrow(stop())
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'nrow': 

nrow appears not to know what to do with the error in this case.

Previously, the uniformity meant that the S4 could be used in place of the base function which Bioconductor packages such as BiocGenerics seem to take advantage of. This persists on the latest development build of R (2020-06-09 r78662). Is this new inconsistency the intended behaviour of R 4.0.1?

Further, the R 4.0.1 behaviour has implications for shiny applications where the new S4 error will terminate/crash the app if encountered in req(nrow(my_reactive())) where the base::nrow and R < 4.0.1 S4 error will not.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • 1
    as a workaround, you could encase your arguments in `tryCatch` and write a new method for signature `x="try-error"`? – JDL Jun 12 '20 at 12:03
  • @JDL thank you, `tryCatch`/`try` does allow for more graceful handling of this case. I am mostly curious if this is acceptable (and broken code should be adjusted accordingly) or if it is a bug in R 4.0.1 that should be taken to [R-devel](https://stat.ethz.ch/mailman/listinfo/r-devel). – Matthew Carlucci Jun 18 '20 at 19:05
  • I'm not sure it's a bug: it's normal for a generic to evaluate all arguments in its signature before choosing a method. If anything, I'm surprised at the *first* example, rather than the second. – JDL Jun 19 '20 at 08:29
  • What I've realized is my true concern is that the 4.0.1 error handling will not preserve attributes of the original error that are useful for shiny applications (shiny.silent.error) and possible other contexts. This crashes many shiny applications using generics in req() statements which depend on the shiny.silent.error attribute. I may edit to expand on this attribute handling in the post. – Matthew Carlucci Jun 24 '20 at 14:28
  • Ah, I can see that might be a problem. Do post an answer if you find out, I'm keen to know! – JDL Jun 25 '20 at 07:54

0 Answers0