我需要从show
方法中恢复隐式用于调用show
的调用的表达式,但这仅适用于显式show show:
> 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")>
打印和S3对象似乎有类似的问题,但我对这里的S4版本更感兴趣。有办法解决这个问题吗?我用sys.calls
查看了调用堆栈,但没有使用原始表达式记录的调用,这表明这可能太低而无法轻易解决。
答案 0 :(得分: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>