我一直在做一个相当雄心勃勃的功能,我希望完成后可以被我以外的人使用。当只有我使用该功能时,我可以忍受输出有点蹩脚,但如果我想要一些好看的输出怎么办?我正在寻找的基本上是这样的:
- 一种打印控制台可读内容的方法
- 能够访问打印的内容
更具体地说,假设我要打印三个标量对象stat
:dfree
和pval
. 目前,我这样做的方式是:
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
),但我找不到是什么让它看起来像这样。
所以我的问题是,我如何在控制台上打印一些漂亮且可读的东西,同时仍然能够从函数中访问单个对象(即结果)?