Dean Attali
2015-Jun-30 07:02 UTC
[Rd] Defining a `show` function breaks the print-ing of S4 object -- bug or expected?
Hi r-devel If you define a function named `show` or attach a package with an exported `show` function, then printing/vieweing S4 objects breaks. This is probably because the `print` function calls `show`, which is now masked. Example: show <- function() {}> setClass("Person", slots = list(name = "character")) > tom <- new("Person", name = "Tom") > tom # error > methods::show(tom) # worksThe error was a surprise to me because I was under the assumption that `show` would be namespaced and therefore should not break. I'm wondering if this is intended behaviour, or if this is a problem. My intuition, and Hadley agreed on Twitter, is that defining a `show` method should not have such a grave effect on printing S4 objects. Thanks --- http://deanattali.com [[alternative HTML version deleted]]
Hadley Wickham
2015-Jun-30 11:57 UTC
[Rd] Defining a `show` function breaks the print-ing of S4 object -- bug or expected?
A slightly simpler formulation of the problem is: show <- function(...) stop("My show!") methods::setClass("Person", slots = list(name = "character")) methods::new("Person", name = "Tom") #> Error in (function (...) : My show! Hadley On Tue, Jun 30, 2015 at 9:02 AM, Dean Attali <daattali at gmail.com> wrote:> Hi r-devel > > If you define a function named `show` or attach a package with an exported > `show` function, then printing/vieweing S4 objects breaks. This is probably > because the `print` function calls `show`, which is now masked. > > Example: > > show <- function() {} >> setClass("Person", slots = list(name = "character")) >> tom <- new("Person", name = "Tom") >> tom # error >> methods::show(tom) # works > > > The error was a surprise to me because I was under the assumption that > `show` would be namespaced and therefore should not break. > I'm wondering if this is intended behaviour, or if this is a problem. My > intuition, and Hadley agreed on Twitter, is that defining a `show` method > should not have such a grave effect on printing S4 objects. > > Thanks > > --- > http://deanattali.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- http://had.co.nz/
Duncan Murdoch
2015-Jun-30 12:20 UTC
[Rd] Defining a `show` function breaks the print-ing of S4 object -- bug or expected?
On 30/06/2015 1:57 PM, Hadley Wickham wrote:> A slightly simpler formulation of the problem is: > > show <- function(...) stop("My show!") > methods::setClass("Person", slots = list(name = "character")) > methods::new("Person", name = "Tom") > #> Error in (function (...) : My show!Just to be clear: the complaint is that the auto-called show() is not methods::show? I.e. after x <- methods::new("Person", name = "Tom") you would expect show(x) to give the error, but not x ?? Duncan Murdoch> > Hadley > > On Tue, Jun 30, 2015 at 9:02 AM, Dean Attali <daattali at gmail.com> wrote: >> Hi r-devel >> >> If you define a function named `show` or attach a package with an exported >> `show` function, then printing/vieweing S4 objects breaks. This is probably >> because the `print` function calls `show`, which is now masked. >> >> Example: >> >> show <- function() {} >>> setClass("Person", slots = list(name = "character")) >>> tom <- new("Person", name = "Tom") >>> tom # error >>> methods::show(tom) # works >> >> >> The error was a surprise to me because I was under the assumption that >> `show` would be namespaced and therefore should not break. >> I'm wondering if this is intended behaviour, or if this is a problem. My >> intuition, and Hadley agreed on Twitter, is that defining a `show` method >> should not have such a grave effect on printing S4 objects. >> >> Thanks >> >> --- >> http://deanattali.com >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel > > >
Reasonably Related Threads
- Defining a `show` function breaks the print-ing of S4 object -- bug or expected?
- Defining a `show` function breaks the print-ing of S4 object -- bug or expected?
- Bug in print.default: dispatches to global show instead of methods::show
- Why no support for 3-digit HEX colours?
- 'vapply' not returning list element names when returned element is a length-1 list