2013-02-23 71 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和打印矩陣進行組合。