[R] function call

Martin Maechler maechler at stat.math.ethz.ch
Mon Jun 19 17:51:13 CEST 2006


>>>>> "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)
   }


Martin Maechler,  ETH Zurich



More information about the R-help mailing list