Question

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.

No correct solution

OTHER TIPS

> 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top