Extract/format/show for S4 objects

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Extract/format/show for S4 objects

Johann Hibschman-3
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Extract/format/show for S4 objects

Gabor Grothendieck
The yearqtr class already rounds off automatically to avoid floating
point effects and handles #1 and #2.

On the other hand if you are just interested in playing around with S4
for its own sake review the source code of the mondate package as an
example of an S4 based date package.

On Tue, Jun 8, 2010 at 10:08 AM, Johann Hibschman <[hidden email]> wrote:

> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Extract/format/show for S4 objects

Jeffrey J. Hallman-2
In reply to this post by Johann Hibschman-3
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 <[hidden email]> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Extract/format/show for S4 objects

Johann Hibschman-3
In reply to this post by Gabor Grothendieck
Gabor Grothendieck <[hidden email]> writes:

> The yearqtr class already rounds off automatically to avoid floating
> point effects and handles #1 and #2.

The main use for all this was so I could use quarters as index columns
with data.table, which demands integer storage mode.  (This is part of
my ongoing attempt to reinvent q/kdb+ in R.)

I've had some problems with yearmon's conversions in the past, so I'm
(probably unfairly) suspicious of the approach of storing dates as
floats.  To be fair, once I reported the bug, it was fixed almost
immediately; I was very impressed.

> On the other hand if you are just interested in playing around with S4
> for its own sake review the source code of the mondate package as an
> example of an S4 based date package.

Thanks, that's a very useful example.  I don't understand all of what's
going on in there, but I find it easier to understand the documentation
with an example in hand.

Cheers,
Johann

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Extract/format/show for S4 objects

Gabor Grothendieck
Note that zoo's merge can handle that:

>> library(zoo)
> z <- zooreg(1:6, as.yearqtr("2000 Q1"))
> merge(z, zlag = lag(z1, -1))
        z  zlag
2000 Q1  1   NA
2000 Q2  2    1
2000 Q3  3    2
2000 Q4  4    3
2001 Q1  5    4
2001 Q2  6    5
2001 Q3 NA    6



On Wed, Jun 9, 2010 at 10:09 AM, Johann Hibschman <[hidden email]> wrote:

> Gabor Grothendieck <[hidden email]> writes:
>
>> The yearqtr class already rounds off automatically to avoid floating
>> point effects and handles #1 and #2.
>
> The main use for all this was so I could use quarters as index columns
> with data.table, which demands integer storage mode.  (This is part of
> my ongoing attempt to reinvent q/kdb+ in R.)
>
> I've had some problems with yearmon's conversions in the past, so I'm
> (probably unfairly) suspicious of the approach of storing dates as
> floats.  To be fair, once I reported the bug, it was fixed almost
> immediately; I was very impressed.
>
>> On the other hand if you are just interested in playing around with S4
>> for its own sake review the source code of the mondate package as an
>> example of an S4 based date package.
>
> Thanks, that's a very useful example.  I don't understand all of what's
> going on in there, but I find it easier to understand the documentation
> with an example in hand.
>
> Cheers,
> Johann
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel