3

I guess this question has been asked somewhere. Although I cannot find one. Please help me out. I need to output a list but only show maybe the first element of the list while the other elements are invisible. Though one is able to access them.

Allow me to give an example.

 s=lm(iris)
 s

 Call:
 lm(formula = iris)

 Coefficients:
       (Intercept)        Sepal.Width       Petal.Length        Petal.Width  
            2.1713             0.4959             0.8292            -0.3152  
 Speciesversicolor   Speciesvirginica  
           -0.7236            -1.0235  

 length(s)
 [1] 13

Here We see that when using lm function, the output is a list of length 13. We can access all the elements we want from s by simply using the dollar sign. But at the same time we only see call and the coefficients, we do not see all the other elements like the residuals, fitted values etc. How can I implement this on my function?

For the example above, The object returned is of class lm. I would like to write a function not necessarily outputting an lm object.

Thank you

Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Are you looking for [this](https://stackoverflow.com/questions/20907583/how-to-return-predicted-values-residuals-r-square-from-lm-fit-in-r)? – akrun Oct 23 '17 at 05:56
  • When you enter `s`, you're actually calling `print()` on the object. There's a function `print.lm()` that specifies what should be shown in this instance, since s has "lm" as its class. It's not part of the exported functions, but you can see what it does via `stats:::print.lm` (note the triple colon operator). – Z.Lin Oct 23 '17 at 06:09
  • @akrun I would like to write a function that only prints a section of my list while the other section is not shown yet it is embedded within the list. Is this possible? – Onyambu Oct 23 '17 at 06:18
  • @Z.Lin can I be able to write a general function that returns an object which is not of class lm, yet be able to just print part of it while the rest is not printed? Please help – Onyambu Oct 23 '17 at 06:19
  • 2
    Within your function, you can define a custom class for your object using something like `class(object) <- "XX"`. Then write your own `print.XX()` function, where XX is the name of your object's class, & specify what you want to be shown when the object is printed. – Z.Lin Oct 23 '17 at 06:25
  • wow. Will look into that. Is there any reference material I can use to get more information on this? Thank you so much. – Onyambu Oct 23 '17 at 06:27

1 Answers1

6

All you have to do is define a new class and the corresponding print method. Something like the following, where I define a class Onyambu.

set.seed(736)    # make the code reproducible
x <- structure(list(
        A = sample(0:1, 20, TRUE),
        B = sample(letters[1:5], 20, TRUE),
        M = 1:5,
        N = 6:10,
        X = rnorm(10),
        Y = rexp(12)
    ),
    class = "Onyambu"  # This is the new class
)

# Before the print method for class "Onyambu" is defined
#   it prints like a normal list
x

print.Onyambu <- function(x){
    cat("Sum: ", sum(x[[1]]), "Mean: ", mean(x[[1]]), "\n")
    print(table(x[[2]]))
    invisible(x)
}

# After print.Onyambu is defined, it prints like we want it to
x
#Sum:  13 Mean:  0.65 
#
#a b c d e 
#3 3 6 5 3
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66