For a long time now I've been using a function: classed <- function (x, cls) { class(x) <- cls x } as a utility at the end other functions to set the class of an object and return the object. I've noticed examples where others have been doing a similar thing and I think it would be a useful addition to R base (unless there is already something there to do this - it wouldn't be the first time I've missed something). The function is useful not so much because it saves a line of code in other functions, but because I think it encourages a cleaner, easier to read style. Paul Gilbert -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
At 10:31 14/09/00 -0400, Paul Gilbert wrote:>For a long time now I've been using a function: > >classed <- function (x, cls) >{ > class(x) <- cls > x >} > >as a utility at the end other functions to set the class of an object andreturn>the object. I've noticed examples where others have been doing a similarthing>and I think it would be a useful addition to R base (unless there is already >something there to do this - it wouldn't be the first time I've missed >something). The function is useful not so much because it saves a line ofcode>in other functions, but because I think it encourages a cleaner, easier toread>style.In a sense it is there already as structure() classed(x, "foo") has exactly the same effect (in R and S3) as structure(x, class = "foo") but structure() is more general allowing several attributes to be set at once via additional arguments (though all have to be named). I should point out, though, that there is a danger in going this way entirely if there is any intention of porting the code to S4 because in S4 the class of an object is NOT an attribute. Once you get into the swing of using structure() in this way it can become habitual and then going through your code to find all the instances where you have set a class attribute this way and changing it for S4 can be quite tedious. Bill Venables.> >Paul Gilbert > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.->r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html >Send "info", "help", or "[un]subscribe" >(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._> >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, Sep 14, 2000 at 10:31:26AM -0400, Paul Gilbert wrote:> classed <- function (x, cls) > { > class(x) <- cls > x > }[snip]> The function is useful not so much because it saves a line of code > in other functions, but because I think it encourages a cleaner, easier to read > style.Just to add my 2c, I don't believe it is cleaner to hide assignment statements inside functions without some reasonably sensible reason to do so. When I see something like: y <- wibble( x, "foo" ); I would expect `x' not to change. Now OK this isn't a watertight rule (nothing ever is) but little functions containing hidden assignment statements are usually confusing to a reader not already familiar with the code. - Tel -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
At 15:00 15/09/00 +1100, Telford Tendys wrote:>On Thu, Sep 14, 2000 at 10:31:26AM -0400, Paul Gilbert wrote: >> classed <- function (x, cls) >> { >> class(x) <- cls >> x >> } >[snip] >> The function is useful not so much because it saves a line of code >> in other functions, but because I think it encourages a cleaner, easierto read>> style. > >Just to add my 2c, I don't believe it is cleaner to hide assignment >statements inside functions without some reasonably sensible reason to >do so. When I see something like: > > y <- wibble( x, "foo" ); > >I would expect `x' not to change.And nor does it. What happens is that a new object is constructed from x, with (in this case) extra bits and pieces tacked on and (here) that new object is assigned the name y. Writing a function to modify its argument is possible (e.g. fix() does it) but not altogether straightforward, especially if you want it to work in the most general case.> Now OK this isn't a watertight rule >(nothing ever is) but little functions containing hidden assignment >statements are usually confusing to a reader not already familiar >with the code.If that was what was happening I would agree with you, but it isn't. You have been misled by the old function/macro thing. What was presented was a true function but you read it as if it were a macro. Bill V. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._