Re: [Rd] Extract/format/show for S4 objects

From: Jeffrey J. Hallman <jhallman_at_frb.gov>
Date: Wed, 09 Jun 2010 07:02:10 -0400

The tis package has the ti (TimeIndex) class that does what you want, and I modestly think it's nicer than the zoo stuff.

As for S4 classes, there's a good reason many of us don't use them: they're too inflexible and hard to program with. I wouldn't go there unless I really had to.

Johann Hibschman <jhibschman_at_gmail.com> writes:

> Hi all,
>
> I'm trying to make an integer-backed quarter (as in fraction of year)
> class, but I can't quite it to work. I want integer-backed so I don't
> have to worry about floating-point effects when doing math, and so that
> I can use it as in data.table.

>
> First of all, is there a good reference for this anywhere? All of the
> S4 tutorials that I've found have been too high-level, and I can't find
> any examples of implementing extract. In S3, I can use [.Date as my
> example, but I can't find the equivalent for S4.

>
> Second, is this misguided?
>
> Now for the details. Given this start:
>
> library(zoo) # borrowing yearqtr for output.
> setClass("iqtr", contains="integer")
> ## Create an iqtr object from a numeric date (e.g. 2000.5).
> iqtr <- function (x) {
> iq <- as.integer(floor((as.numeric(x) - 2000) * 4 + 0.0001))
> new("iqtr", iq)
> }
> setMethod("show", "iqtr4",
> function (object) {
> cat(format(yearqtr(as.numeric(x) * 0.25 + 2000)))
> })
>
>
> I have two issues:
>
> 1. Vectors of iqtrs do not display properly.
> They print, but they don't look like vectors.
> > iq <- iqtr(seq(2000, 2002, 0.25))
> > iq
> 2000 Q2 2000 Q2 2000 Q3 2000 Q4 2001 Q2 2002 Q1>
>
> 2. Subsets do not stay in the class.
> > iq[1:2]
> > [1] 0 1
>
> Clearly, I'm just not understanding what's going on. Is there a guide
> for how to make printing, format, subsetting, etc., all work for S4
> classes?
>
> Thanks,
> Johann
>

-- 
Jeff

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 09 Jun 2010 - 11:04:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jun 2010 - 16:41:00 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive