R 创建美观的输出

R 创建美观的输出,r,function,output,R,Function,Output,我一直在致力于一个雄心勃勃的功能,我希望在我完成后可以被其他人使用。当只有我使用这个函数时,我可以忍受输出有点蹩脚,但是如果我想要一些好看的输出呢?我要找的基本上是: 向控制台打印可读内容的一种方法 能够访问打印的内容 更具体地说,让我们假设我有三个要打印的标量对象:stat、dfree和pval。目前,我的做法是: result <- list(statistic = stat, degrees = dfree, p.value = pval) return(result) 它可以

我一直在致力于一个雄心勃勃的功能,我希望在我完成后可以被其他人使用。当只有我使用这个函数时,我可以忍受输出有点蹩脚,但是如果我想要一些好看的输出呢?我要找的基本上是:

  • 向控制台打印可读内容的一种方法
  • 能够访问打印的内容
更具体地说,让我们假设我有三个要打印的标量对象:
stat
dfree
pval
。目前,我的做法是:

result <- list(statistic = stat, degrees = dfree, p.value = pval)
return(result)
它可以工作,但是输出有点难看

> 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
# 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 
>库(vars)
>数据变量(数据,p=2,type=“trend”)
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
),但是我找不到什么使它看起来像这样


因此,我的问题是,如何在仍然能够从函数访问单个对象(即结果)的情况下,向控制台打印一些漂亮且可读的内容?

您应该给结果一个类,比如“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>
vars:::print.varest
#---------------
函数(x,数字=最大值(3,getOption(“数字”)-3),…)
{

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

> 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
# 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 
#设置类名,其表示形式在此处列出。
setClass(“stat_test”,表示法(“列表”))
#show方法(以下是输出的打印方式
#您可以格式化为任何您想要的…显示和如何显示
setMethod(“显示”、“统计测试”、函数(对象){
类别(“对象”,类别(对象),“\n”)
cat(“估计系数\n”)
cat(“统计\t\t\t等级\t\t\tp.value\n”)
cat(“,对象$statistics,”\t\t\t“,对象$degrees,“\t\t\t”,对象$p.value,“\n”)
})
#现在是实际函数(这里当然是虚拟函数)

我的乐趣增加了@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' )
#运行示例代码
图书馆(vars)

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


其他有助于实现漂亮但简单的输出的方法是,将您想要的东西放在屏幕上的矩阵中,给出矩阵的行名称和列名,然后打印矩阵和打印。矩阵函数将处理好排列。有些函数将使用
cat
和打印矩阵相结合。

我看不出有什么好处你的输出很难看。你在比较两个完全不同的东西,如果你给出一个例子,说明你真正需要的输出是什么,包括样本数据,你可能会得到更好的答案。很好,这解释了一些问题(特别是现在我可以实际查看varest的输出是如何创建的!)。这也许是一个愚蠢的问题,但我如何为我的新类创建一个打印方法(比如说“resclass”)?比如说我希望它打印“这是p值:”使用这种方式,而不是简单地打印(粘贴(“这是p值:”,pval,sep=”“)。我该怎么做?太好了!非常感谢,这是一个完美的基本代码,可以从开始开始,然后扩展。太棒了:)谢谢!非常有用。我想我现在知道了基本想法,所以现在我知道从哪里开始了。