创造美观的输出 [英] Creating nice looking output
问题描述
我一直在研究一个相当雄心勃勃的职能,我希望一旦完成,我希望能被其他人使用。当它只是我使用的功能,我可以忍受的输出是一种跛脚,但如果我想要一些漂亮的输出?我正在寻找的基本上是这样的:
$ ul
更具体地说,假设我想要打印三个标量对象:
stat
, dfree
和 pval
。目前,我这样做的方式是: 结果< - list(statistic = stat,degrees = dfree,p.value = pval)
return(result)
这样我就可以通过运行来访问这些值,例如(该函数称为 whites.htest
):
白人.htest $ p.value
它可以工作,但输出很糟糕。
> whites.htest(var.modell)
$ statistic
[1] 36.47768
$ degrees
[1] 30
$ p .value
[1] 0.1928523
如果我们像这样运行一个简单的VAR模型: / p>
>图书馆(变量)
>数据< - 矩阵(rnorm(200),ncol = 2)
> VAR(数据,p = 2,type =趋势)
VAR估计结果:
==================== ===
等式y1的估算系数:
============================= ==========
电话:
y1 = y1.l1 + y2.l1 + y1.l2 + y2.l2 +趋势
y1.l1 y2.l1 y1.l2 y2.l2趋势
-0.090102007 -0.060138062 0.126250484 0.014423006 0.003138521
等式y2的估计系数:
======= ================================
电话:
y2 = y1.l1 + y2。 l1 + y1.l2 + y2.l2 +趋势
y1.l1 y2.l1 y1.l2 y2.l2趋势
0.040118527 0.018274399 -0.132943318 -0.031235939 0.003242241
输出看起来非常好。我已经查看了它的底层代码(通过简单地运行 VAR
),但我找不到像这样好看的东西。
所以我的问题是,如何在控制台上打印出好而易读的东西,同时仍然可以从函数中访问单个对象(即结果)?
如果您要编写更多的函数,我可以想到的一种方法是对输入进行优化(并获得更多控制权),即创建一个类并修改 show
方法..就像这样:
#set您的班级名称及其表示形式在此处列出。
setClass(stat_test,representation(list))
#show method(这里是输出结果如何打印
#你想要显示和显示
setMethod(show,stat_test,function(object){
cat(object of,class(object),\\\
)
cat(Estimated Coefficients \\\
)
cat(statistics \t\t\tdegrees\t\t\tp.value\\\
)
cat(,object $ statistics,\t\t\t,object $ degrees,\t\t\t,object $ p.value,\\\
)
})
#现在你的实际函数(这里是虚拟的)
my_fun< - function(x){
t< - list(statistics = 1.5,degrees = 30,p.value = 1e-2)
new(stat_test,t)
}
#现在调用
w< - my_fun(2)
> w#您获得
stat_test的对象
估计系数
统计度p.value
1.5 30 0。 01
当然,您应该注意对齐。但这是一个基本的想法。
I've been working on quite an ambitious function, which I hope can be used by people other than me once I am finished. When it's just me using the function I can live with the output being kind of lame, but what if I want some nice looking output? What I'm looking for is essentially this:
- A way of printing something readable to the console
- Being able to access what's printed
More specifically, let's assume I have three scalar objects I want to be printed: stat
, dfree
and pval
. Currently, the way I do it is:
result <- list(statistic = stat, degrees = dfree, p.value = pval)
return(result)
That way I can access these values by running, for example (the function is called whites.htest
):
whites.htest$p.value
It works, but the output is kind of ugly.
> whites.htest(var.modell)
$statistic
[1] 36.47768
$degrees
[1] 30
$p.value
[1] 0.1928523
If we run a simple VAR model like this:
> 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
The output looks really good. I've had a look at the underlying code for it (by simply running VAR
), but I cannot find what makes it look good like this.
So my question is, how do I print something nice and readable to the console while still being able to access individual objects (i.e. results) from the function?
One way I could think of to prettify the input (and gain more control if you're writing more functions) is to create a class and modify the show
method.. Something like this:
# 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
You should take care of the alignments of course. But this is one basic idea.
这篇关于创造美观的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!