Hi Christofer, I think this might not be the best use of S4 methods (I am still at the constantly rereading the R extensions manual and John Chambers' book stage), but since you are just printing numeric class data, I would be tempted to use set it up so your method just passes it on to the default. Of course, this does not allow for different number of digits for x1 and x2, but is that really necessary anyways? People can always delete a couple if its too precise from one. If you really want to tweak both, I think you'd need to add digits1 and digits2 arguments or something, and maybe use round or some other method to edit those values _before_ you cat() them.
I'd love to hear alternative suggestions! Cheers, Josh (code follows) ###################################### setClass("Me", representation( x1 = "numeric", x2 = "numeric", y1 = "character") ) setMethod("print", "Me", definition = function (x, ...) { cat("Values of x1 & x2 are: \n") callNextMethod(x = c(x@x1, x@x2), ...) }) new1 = new("Me", x1=rnorm(2), x2=rt(2, 1), y1="normal") print(new1, digits = 10) print(new1, digits = 2) ###################################### On Mon, Feb 7, 2011 at 9:36 AM, Bogaso Christofer <bogaso.christo...@gmail.com> wrote: > I have got another question on defining a method for print() function for my > s4 class. Here is my class definition as well as what I have done till now: > > > >> setClass("Me", representation(x1 = "numeric", x2 = "numeric", y1 = > "character")) > > [1] "Me" > >> setMethod("print", "Me", definition=function(x) { > > + cat("Values of x1 & x2 are:", c(x@x1, x@x2), "\n") > > + }) > > [1] "print" > >> new1 = new("Me", x1=rnorm(2), x2=rt(2, 1), y1="normal") > >> print(new1) > > Values of x1 & x2 are: -2.139669 -0.2102133 -0.6293572 -26.96205 > > > > However what I wanted to have that, user should have some option to print > the underlying object with as much accuracy as he wants, in terms of > displaying the significant digits, for example prints number of user defined > precision: > > > >> print(rnorm(2), 10) > > [1] -0.39146522347 -0.05624702385 > >> print(rnorm(2), 5) > > [1] 0.34575 0.87486 > > > > Can somebody help me how can I get such options for my class? Additionally I > want to have 2 different additional arguments for print() function of my > class, so that "x1" and "x2" will be displayed in different significant > digits. > > > > Thanks and regards, > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.