如何让“摘要"与 R 中的自定义类一起使用 [英] How to get 'summary' to work with custom class in R

查看:53
本文介绍了如何让“摘要"与 R 中的自定义类一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何让 summary(object) 与我正在创建的包中的自定义类一起使用.例如,如果您运行以下命令:

I'm wondering how I can get the summary(object) to work with a custom class in a package I'm creating. For example, if you run the following:

testfunction <- function(x) {
  x.squared <- x^2
  x.double <- 2*x
  x.triple <- 3*x

  result <- list(squared = x.squared, double = x.double, triple = x.triple)
  class(result) <- "customclass"
  result
}

x <- rnorm(100)
output <- testfunction(x)
summary(output)

你会看到输出是非常无用的.但是,我似乎无法找到如何控制此输出.如果有人可以指导我做某事,我将不胜感激.

you will see that the output is quite useless. I can't, however, seem to find how to control this output. If someone could direct me towards something I'd be grateful.

(当然,我可以创建一个自定义的汇总函数,例如 summary.Custom(object),但我更喜欢使用常规的 summary 方法直接.

(I could, of course, make a custom summary function such as summary.Custom(object), but I'd prefer to have the regular summary method working directly.

推荐答案

编写一个名为 summary.customclass 的函数,其参数与 summary 相同(请参阅 args(摘要)).

Write a function called summary.customclass with the same arguments as summary (see args(summary) for that).

您正在做的是为 S3 类创建一个 summary 方法.您可能想阅读 S3 课程.

What you are doing there is creating a method of summary for an S3 class. You might want to read up on S3 classes.

这篇关于如何让“摘要"与 R 中的自定义类一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆