[Rd] classed

Bill Venables Bill.Venables@cmis.csiro.au
Fri, 15 Sep 2000 09:31:11 +1000


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 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.

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._