[R] function call

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jun 20 02:36:03 CEST 2006


On 6/19/06, Martin Maechler <maechler at stat.math.ethz.ch> wrote:
> >>>>> "vincent" == vincent  <vincent at 7d4.com>
> >>>>>     on Mon, 19 Jun 2006 16:30:54 +0200 writes:
>
>    vincent> Gabor Grothendieck a écrit :
>    >> Perhaps you what you want to do is to return an object
>    >> that has a print method like this:
>
>    >> f1 <- function(x) structure(c(x, x^2), class = "f1")
>    >> print.f1 <- function(x) cat("x is", x[1], "x squared is", x[2], "\n")
>
>    vincent> Thank you Gabor for this idea.
>
> Unfortunately, it's not a good idea inspite of coming from Gabor who
> has donated many very good ideas to R-help:
>
> help(print)  tells you
>
>  >>   'print' prints its argument and returns it _invisibly_ (via
>  >>   'invisible(x)').  It is a generic function which means that new
>  >>   printing methods can be easily added for new 'class'es.
>
> I.e., as a good Ritizen, Gabor's function above should **REALLY** be
>
>   print.f1 <- function(x) {
>          cat("x is", x[1], "x squared is", x[2], "\n")
>          invisible(x)
>   }
>

ok, in that case perhaps it would be better to define a summary
method.

f1 <- function(x) structure(c(x, x^2), class = "f1")
summary.f1 <- function(x) c(x = x[1], xsquared = x[2], sum = sum(x))

out <- f1(3)
out
unclass(out)
summary(out)



More information about the R-help mailing list