Re: [R] S3 and S4 classes
I've made some changes (in bold) following your comments : # class definition : setClass(MyClass, representation(mynumber=numeric)); # the initilization method : setMethod(initialize,MyClass, function(.Object) { [EMAIL PROTECTED] - 10; return(.Object); } ); # defining the generic function generates a warning message (already generic function)- but everything works fine without it - so it seems better not to use it *setGeneric(perform, function(object) standardGeneric(perform)); * # the perform method definition - this alone seems sufficient for me, and S4-looking *setMethod(perform,MyClass,** function(object) { [EMAIL PROTECTED] - [EMAIL PROTECTED] + 10; return(object); } **);* I replaced my unlucky /.Object/ by /object/ but for method initialize. The execution of my program used to begin with an output like : Creating a new generic function for perform With the setMethod only, there is not this output any longer. Does this mean that the execution becomes faster ? Thank you for your fast and useful answers. I've found an interesting R help page I had never seen before at http://www.maths.lth.se/help/R/S3toS4 which gives additionnal information about S3 and S4 classes. Regards, Laurent http://www.maths.lth.se/help/R/S3toS4/ [[alternative HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
RE: [R] S3 and S4 classes
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Laurent Faisnel Sent: den 24 juli 2003 14:04 To: John Chambers Cc: [EMAIL PROTECTED]; Duncan Murdoch Subject: Re: [R] S3 and S4 classes [snip] Thank you for your fast and useful answers. I've found an interesting R help page I had never seen before at http://www.maths.lth.se/help/R/S3toS4 which gives additionnal information about S3 and S4 classes. Beware that I haven't updated this page since early 2002, it is based on early versions of the 'methods' package and my early understandings of it and is likely to be out of date (I'll put a note about this on the page as soon as I get access to the server). Regards, Laurent Have a nice day! Henrik Bengtsson Lund University __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] S3 and S4 classes
On Wed, 23 Jul 2003 14:53:56 +0200, Laurent Faisnel [EMAIL PROTECTED] wrote : Could anyone point me out what's S3-like in the following sample and why it is not fully S4-compatible ? # a function that objects of this class have perform - function(.Object) UseMethod(perform, .Object); It think this is unnecessary, and somewhat S3-like. A more S4-looking way to do the same (?) thing is setGeneric(perform, function(.Object) standardGeneric(perform)) but I think this will be generated automatically when you define your methods. Duncan Murdoch __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] S3 and S4 classes
Duncan Murdoch wrote: On Wed, 23 Jul 2003 14:53:56 +0200, Laurent Faisnel [EMAIL PROTECTED] wrote : Could anyone point me out what's S3-like in the following sample and why it is not fully S4-compatible ? # a function that objects of this class have perform - function(.Object) UseMethod(perform, .Object); It think this is unnecessary, and somewhat S3-like. A more S4-looking way to do the same (?) thing is setGeneric(perform, function(.Object) standardGeneric(perform)) but I think this will be generated automatically when you define your methods. As Duncan says, this is the S3-style portion of the example. It's not wrong, but there are advantages to NOT going this route. The UseMethod() call says that this is a function with S3-style methods. Is that true? It might well be--you could have a function perform.default, for example, that was the default method to use. The disadvantage of hanging on to S3 methods is that they're hidden; unlike S4 methods, you can't easily find out what methods are defined (by calling showMethods()). If you don't have any existing definition of perform(), you will need to call setGeneric() as Duncan showed. The implication is that perform() doesn't have a default method--unless the argument inherits from one of the classes in a setMethod() call, the result is an error. (If there is a non-generic version of perform, that becomes the default method, as it would in your example.) If you DID have a perform.default, you might want to make that explicitly the S4 default method setMethod(perform, ANY, perform.default) after the setGeneric call. Similarly, you could make other S3 methods into S4 methods. Then all the methods are visible. Also, a point of good style, unrelated to methods. It's not generally a good idea to have function arguments starting with .. Names of this form are intended for behind-the-scenes manipulations. By sticking to names that start with a letter, you avoid the chance of conflicting with some such manipulation. So, Object rather than .Object. (The reason intialize() uses .Object is exactly BECAUSE it expects user-defined arguments, in the ..., to start with a letter, and so chooses .Object to minimize the chance of conflicting.) Regards, John Chambers Duncan Murdoch __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help -- John M. Chambers [EMAIL PROTECTED] Bell Labs, Lucent Technologiesoffice: (908)582-2681 700 Mountain Avenue, Room 2C-282 fax:(908)582-3340 Murray Hill, NJ 07974web: http://www.cs.bell-labs.com/~jmc __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help