Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does print of S4 class call `show` without namespacing it?

Tags:

r

s4

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

like image 210
DeanAttali Avatar asked Jun 29 '15 23:06

DeanAttali


People also ask

How do you create S4 objects How can you check an object is S4 object?

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.

What is a S4 object in R?

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).

What is setClass?

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).

What are S3 and S4 classes?

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.


1 Answers

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

like image 162
DeanAttali Avatar answered Oct 16 '22 17:10

DeanAttali