10

I have a package shinyjs with a function called show. Today a user reported to me that this introduces problems when using S4 objects because "print"-ing an S4 object uses the show method, which is masked by my package when it's attached.

Example:

library(shinyjs)
setClass("testS4Object",
         representation(
           ID = "numeric",
           Name = "character"
         ),
         prototype(
           ID = NA_real_,
           Name = NA_character_
         )
)
x = new("testS4Object")
x

There's an error because when we print the value of x, it seems to call show under the scenes, but it's using shinyjs::show instead of methods::show. By printing methods::show(x) explicitly, the problem goes away. But I'm a bit confused as to why by default the S4 printing system calls show without namespacing it - isn't it dangerous and not really my package's fault that this bug is happening?

It is considered a very bad idea to have a function with the same name as a function in methods? My thinking is that the S4 system should know to call their own show function or an inherited S4 show function.

EDIT: I asked Hadley what he thinks and he seems to also think this might be a bug in R, I emailed r-devel to get their opinion

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • 1
    Is `show` intended to be used by clients? If not, would *not* exporting it (e.g. in your `NAMESPACE` file) affect the functionality of other functions in your package? – nrussell Jun 30 '15 at 00:00
  • It is intended to be used. It's the most popular function in the package. – DeanAttali Jun 30 '15 at 00:31
  • It would probably be easiest to rename in then. – nrussell Jun 30 '15 at 00:38
  • So am I to understand that defining a `show` method is just as dangerous as defining a `print` method? This is intended behaviour of printing S4? – DeanAttali Jun 30 '15 at 00:46
  • I believe that's correct. I'm not completely sure about this, but I think the issue stems mainly from the fact that `methods` is loaded automatically, so your exported `show` function is being found first on R's search path. But I'm semi-speculating here, so it's probably best to hold off on making any changes until someone can give you a better answer. Good question though. – nrussell Jun 30 '15 at 00:53
  • thanks, will wait to hopefully see more answers – DeanAttali Jun 30 '15 at 01:03

1 Answers1

2

The issue was reported to the R core team and was fixed on 2015-07-20 in SVN commit # 68702. Here is the fix

The fix will be available in R 3.3.0

DeanAttali
  • 25,268
  • 10
  • 92
  • 118