How to get a "resume" for working with a custom class in R

I am wondering how I can get summary(object) to work with a custom class in the package I create. 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 conclusion is completely useless. However, I cannot find how to manage this output. If someone could direct me to something, I would be grateful.

(I could, of course, create a custom summary function such as summary.Custom(object) , but I would prefer the regular summary method to work directly.

+4
source share
2 answers

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

What you do is create a summary method for class S3. You might want to read on S3 classes.

+4
source

There is no summary.list function. If you want to use the summary.default function, you need to use lapply or sapply :

 > lapply(output, summary) $squared Min. 1st Qu. Median Mean 3rd Qu. Max. 0.000013 0.127500 0.474100 1.108000 1.385000 11.290000 $double Min. 1st Qu. Median Mean 3rd Qu. Max. -6.7190 -1.0480 0.4745 0.3197 1.8170 5.1870 $triple Min. 1st Qu. Median Mean 3rd Qu. Max. -10.0800 -1.5720 0.7117 0.4796 2.7260 7.7800 

Or:

 > sapply(output, summary) squared double triple Min. 1.347e-05 -6.7190 -10.0800 1st Qu. 1.275e-01 -1.0480 -1.5720 Median 4.741e-01 0.4745 0.7117 Mean 1.108e+00 0.3197 0.4796 3rd Qu. 1.385e+00 1.8170 2.7260 Max. 1.129e+01 5.1870 7.7800 
+2
source

Source: https://habr.com/ru/post/1501168/


All Articles