[Rd] Extract/format/show for S4 objects

From: Johann Hibschman <jhibschman_at_gmail.com>
Date: Tue, 08 Jun 2010 09:08:53 -0500


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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 08 Jun 2010 - 14:23:17 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 - 14:21:01 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