R: Hiding info in the output of a function (like how the lm function works)

Viewed 114

So a quick example of the output from the lm function:

> y<-rnorm(10)
> x<-y+rnorm(10)
> L<-lm(y ~ x)       #Creates the object L
> L                  #Gives only a small summary of L

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept)            x  
    0.03514      0.77202  

> residuals(L)             # Gives all the raw data on the residuals of L
           1            2            3            4            5 
 0.379071438  0.357077188 -0.270606886 -0.138712332  0.167195314 
           6            7            8            9           10 
-0.017580418 -0.859508221 -0.008648384  0.532864345 -0.141152045 
> 

So what's going on here? "L" is the output of the lm function, and by entering L into the console it gives a summary. However, when outputting residuals(L), it gives additional information not found in L. Somehow, the object "L" contains this information but it is obscured. I would like to figure out how to do this in my code.

How do I go about creating an object "Myobject" that, when called, only gives a summary, but when applying some function "moredetails(Myobject)" gives additional details.

I can see this being very handy when it comes to complicated analyses, yet I can't find anything in the source code for lm that would indicate how this is done. I also feel like I'm missing a keyword which would make searching for this problem much easier.

0 Answers
Related