0

I need to recover the expression implicitly used to invoke a call to show from within a show method, but this only works with an explicit show call:

> setClass("test", representation(a="character"))
> setMethod("show", "test", function(object) cat(deparse(substitute(object))))
[1] "show"
> show(new("test"))    # explicit call: as expected
new("test")
> new("test")          # implicit: not so much...
<S4 object of class structure("test", package = ".GlobalEnv")>

There seems to be a similar issue with print and S3 objects, but I'm more interested in the S4 version here. Any way to work around this? I looked at the call stack with sys.calls but there was no call recorded with the original expression which suggests to me this may be too low level to be resolved easily.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • why do you need to recover calls for implicit print ? This is only in interactive sessions. Here it' s the R console that prints your object. – Karl Forner Dec 29 '13 at 10:11
  • I'm building an interactive interface that involves users manipulating S4 objects interactively. – BrodieG Dec 29 '13 at 20:24
  • ok, but that still does not really answer the question: is there console from which you want to recover implicit calls to show ? – Karl Forner Jan 02 '14 at 10:08
  • I'm doing this from RStudio, but ideally would like the solution to work on any console so that the package that incorporates this approach is not UI dependent. – BrodieG Jan 02 '14 at 12:56

1 Answers1

0
> showDefault
function (object, oldMethods = TRUE) 
{
    clDef <- getClass(cl <- class(object), .Force = TRUE)
    cl <- classLabel(cl)
    if (!is.null(clDef) && isS4(object) && is.na(match(clDef@className, 
        .BasicClasses))) {
        cat("An object of class ", cl, "\n", sep = "")
        slots <- slotNames(clDef)
        dataSlot <- .dataSlot(slots)
        if (length(dataSlot) > 0) {
            dataPart <- slot(object, dataSlot)
            show(dataPart)
            slots <- slots[is.na(match(slots, dataSlot))]
        }
        else if (length(slots) == 0L) 
            show(unclass(object))
        for (what in slots) {
            if (identical(what, ".Data")) 
                next
            cat("Slot \"", what, "\":\n", sep = "")
            print(slot(object, what))
            cat("\n")
        }
    }
    else print(object, useS4 = FALSE)
    invisible()
}
<bytecode: 0x11c228000>
<environment: namespace:methods>
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I'm not sure how this answers my question. I can call `showDefault` on my S4 object, sure, but the whole point is that I want to capture the expression the implicit show/print used (i.e. what someone typed at the prompt) from the show/print method. Maybe there is something above here that does this, but it's not obvious to me. – BrodieG Dec 29 '13 at 20:28