问题
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
回答1:
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
来源:https://stackoverflow.com/questions/31127056/why-does-print-of-s4-class-call-show-without-namespacing-it