2013-02-23 74 views
3

我一直在研究一个相当雄心勃勃的功能,我希望一旦完成,我希望可以被其他人使用。当它只是我使用的功能,我可以忍受的输出是一种跛脚,但如果我想要一些漂亮的输出?我正在寻找的本质是这样的:创建漂亮的输出

  • 打印的东西可读控制台
  • 能够访问什么印刷

更具体地说,假设我有三个标对象的方法我想要打印:stat,dfreepval。目前,我做的方式是:

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),但我无法找到使它看起来像这样的好东西。

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

+0

我看不出你的输出有什么不好。你正在比较两个完全不同的东西,如果你举例说明你真正需要输出的东西,包括样本数据,你可能会得到更好的答案。 – N8TRO 2013-02-23 23:05:21

回答

5

一个我能想到的美化输入(如果你正在写更多的功能获得更多的控制)的方式是创建一个类,并修改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 

你应该照顾当然的路线。但这是一个基本的想法。

+1

太棒了!非常感谢,这是一个完美的基本代码,从头开始,然后展开。辉煌:) – hejseb 2013-02-23 23:24:54

4

你应该给你的结果一个类,说“resclass”并创建一个print.resclass函数。 print是一个通用函数,它将搜索print.resclass的函数空间并将其应用于您的对象。您可以让print方法返回NULL或让它不可见地返回对象值。通常的做法是重复调用cat。我看到阿伦已经提供了一个例子。从包装作者那里学习总是可能的。这里的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> 
+0

谢谢!很有帮助。我想我现在已经有了基本想法,现在我知道从哪里开始。 – hejseb 2013-02-23 23:30:54

1

增加@迪文的回答..

# 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') 
+0

不错,这解释了一些(特别是现在我实际上可以查看如何创建最近的输出!)。也许是一个愚蠢的问题,但是如何为我的新类创建一个打印方法(比如说“resclass”)?假设我希望它打印出“这是p值:<< p.value在这里>>”使用这种方式,而不是简单地打印(粘贴(“这是p值:”,pval,sep = “”))。我会怎么做? – hejseb 2013-02-23 23:19:40

1

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

其他有助于输出更好,更简单的东西的方法是将屏幕上想要的东西放在矩阵中,并给出矩阵行名称和列名称,然后打印矩阵和print.matrix函数好好照顾好事情。一些功能将使用cat和打印矩阵进行组合。