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
Example 2: Creation of S4 object We can check if an object is an S4 object through the function isS4() . The function setClass() returns a generator function. This generator function (usually having same name as the class) can be used to create new objects. It acts as a constructor.
The S4 system in R is a system for object oriented programing. Confusingly, R has support for at least 3 different systems for object oriented programming: S3, S4 and S5 (also known as reference classes).
A set class is a group of pitch-class sets related by transposition or inversion. Set classes are named by their prime form : the version of the set that is transposed to zero and is most compact to the left (compared with its inversion).
There are mainly two major systems of OOP, which are described below: S3 Classes: These let you overload the functions. S4 Classes: These let you limit the data as it is quite difficult to debug the program.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With