4

我一直在做一个相当雄心勃勃的功能,我希望完成后可以被我以外的人使用。当只有我使用该功能时,我可以忍受输出有点蹩脚,但如果我想要一些好看的输出怎么办?我正在寻找的基本上是这样的:

  • 一种打印控制台可读内容的方法
  • 能够访问打印的内容

更具体地说,假设我要打印三个标量对象statdfreepval. 目前,我这样做的方式是:

result <- list(statistic = stat, degrees = dfree, p.value = pval)
return(result)

这样我就可以通过运行来访问这些值,例如(调用函数whites.htest):

whites.htest$p.value

它有效,但输出有点难看。

> whites.htest(var.modell)
$statistic
[1] 36.47768

$degrees
[1] 30

$p.value
[1] 0.1928523

如果我们像这样运行一个简单的 VAR 模型:

> library(vars)
> data <- matrix(rnorm(200), ncol = 2)
> VAR(data, p = 2, type = "trend")

VAR Estimation Results:
======================= 

Estimated coefficients for equation y1: 
======================================= 
Call:
y1 = y1.l1 + y2.l1 + y1.l2 + y2.l2 + trend 

       y1.l1        y2.l1        y1.l2        y2.l2        trend 
-0.090102007 -0.060138062  0.126250484  0.014423006  0.003138521 


Estimated coefficients for equation y2: 
======================================= 
Call:
y2 = y1.l1 + y2.l1 + y1.l2 + y2.l2 + trend 

       y1.l1        y2.l1        y1.l2        y2.l2        trend 
 0.040118527  0.018274399 -0.132943318 -0.031235939  0.003242241

输出看起来非常好。我已经查看了它的底层代码(通过简单地运行VAR),但我找不到是什么让它看起来像这样。

所以我的问题是,我如何在控制台上打印一些漂亮且可读的东西,同时仍然能够从函数中访问单个对象(即结果)?

4

4 回答 4

6

我能想到的一种美化输入的方法(如果你编写更多的函数,可以获得更多的控制)是创建一个类并修改show方法。像这样的东西:

# set your class name and its representation is list here.
setClass( "stat_test", representation("list"))


# show method (here's how the output would be printed
# you can format to whatever you want... to show and how to show
setMethod("show", "stat_test", function(object) {
    cat("object of", class(object), "\n")
    cat("Estimated Coefficients\n")
    cat("  statistics\t\t\tdegrees\t\t\tp.value\n")
    cat("  ", object$statistics, "\t\t\t", object$degrees, "\t\t\t", object$p.value,"\n")
})


# now your actual function (here dummy of course)
my_fun <- function(x) {
    t <- list(statistics=1.5, degrees=30, p.value=1e-2)
    new("stat_test", t)
}

# now calling
w <- my_fun(2)
> w # you get

object of stat_test 
Estimated Coefficients
  statistics            degrees         p.value
  1.5            30              0.01 

当然,您应该注意对齐方式。但这是一个基本的想法。

于 2013-02-23T23:11:05.783 回答
4

你应该给你的结果一个类,说“resclass”并创建一个print.resclass函数。print是一个通用函数,它将搜索函数空间print.resclass并将其应用于您的对象。您可以让 print 方法返回 NULL 或让它不可见地返回对象值。通常的做法是重复调用cat. 我看到 Arun 已经提供了一个例子。总是可以向你的包作者学习。这print.varest是您欣赏的功能:

 vars:::print.varest
#---------------
function (x, digits = max(3, getOption("digits") - 3), ...) 
{
    dim <- length(x$varresult)
    names <- colnames(x$y)
    text1 <- "VAR Estimation Results:"
    cat(paste("\n", text1, "\n", sep = ""))
    row <- paste(rep("=", nchar(text1)), collapse = "")
    cat(row, "\n")
    cat("\n")
    for (i in 1:dim) {
        result <- coef(x$varresult[[i]])
        text1 <- paste("Estimated coefficients for equation ", 
            names[i], ":", sep = "")
        cat(text1, "\n")
        row <- paste(rep("=", nchar(text1)), collapse = "")
        cat(row, "\n")
        text2 <- paste("Call:\n", names[i], " = ", paste(names(result), 
            collapse = " + "), sep = "")
        cat(text2, "\n\n")
        print(result, ...)
        cat("\n\n")
    }
    invisible(x)
}
<environment: namespace:vars>
于 2013-02-23T23:08:22.220 回答
1

添加到@DWin的答案..

# run your example code
library(vars)
data <- matrix(rnorm(200), ncol = 2)
# store the output of `x`
x <- VAR(data, p = 2, type = "trend")

# what kind of object is `x`?
class( x )

# look at code that the author of the `vars`
# package wrote for the print method
getS3method( 'print' , 'varest' )

# look at others..
getS3method( 'print' , 'varsum' )

# ..and others
methods( 'print' )
于 2013-02-23T23:11:13.210 回答
1

通常的做法是将函数的返回值分配给给定的类(您选择类的名称),然后为该类创建一个打印方法,该方法将很好地格式化输出(通常使用cat)并返回相同的无形的对象。通常还有一个 summary 方法和一个 print.summary 方法来提供额外的输出。

其他可以帮助输出很好但很简单的事情是将屏幕上您想要的东西放在一个矩阵中,并给出矩阵的行名和列名,然后打印矩阵,print.matrix 函数将处理衬里事情进展顺利。一些函数将结合使用cat和打印矩阵。

于 2013-02-23T23:13:19.573 回答