[Rd] setAs between a new S4 class and "POSIXlt"

Christoph Buser buser at stat.math.ethz.ch
Tue Mar 29 11:08:48 CEST 2005


Dear R core team

Please apologize for posting the same question twice on R-help and
R-devel. Since I was not sure which list is appropriate I tried R-help (Tue
Mar 22), but got no answer.  
Now I do not know if the formulation of my question was unclear or the
question is not so easy to answer or to easy (what I do not hope).

My problem: I create a new S4 class, containing one slot, data (of class
	    POSIXlt). I tried to define a transformation via setAs()
	    between the new S4 class and the existing "POSIXlt" class that is 
	    virtual S4 Class with subclass "POSIXt".
	    Then using as() gives an error message (see code below) when I
	    transform from "POSIXlt" to the new class.
	    Then I defined another as() from the subclass "POSIXt" to the
	    new class and now everything works well.

This is the point where I do not understand what happens and why it works
like this. I'd be happy for some hints to improve my understanding (maybe
there is a help page or a reference).
I'd be happy with a confirmation of a better connoisseur of R than me, that
it is not so easy to understand, but correct, if that's the case. Thank you
very much.
Below I attached my R code.

Regards, 

Christoph Buser

--------------------------------------------------------------
Christoph Buser <buser at stat.math.ethz.ch>
Seminar fuer Statistik, LEO C11
ETH (Federal Inst. Technology)	8092 Zurich	 SWITZERLAND
phone: x-41-1-632-5414		fax: 632-1228
http://stat.ethz.ch/~buser/
--------------------------------------------------------------

## R Code:

## Definition of my new S4 class
setClass("dtime",
         representation(data = "POSIXlt"),
         prototype(data = as.POSIXlt("2004/06/01")))

## Transformation between the new class "dtime" and "POSIXlt"
setAs("dtime", "POSIXlt", def = function(from) {
  from at data
})
setAs("POSIXlt", "dtime", def = function(from) {
  new("dtime", data = from)
})

## Create a new "dtime" object
(x1 <- new("dtime"))
str(x1)
## Transformation from "dtime" to "POSIXlt" works well
(y1 <- as(x1, "POSIXlt"))
str(y1)

## Create a new "POSIXlt" object
(y2 <- as.POSIXlt("2004/06/01"))
str(y2)
## Transformation from "POSIXlt" to "dtime" fails
as(y2, "dtime")
> Fehler in insertMethod(methods, sig, args, def, TRUE) : 
	inserting method into non-methods-list object (class "NULL")

## This works properly
new("dtime", data = y2)

## Now I put another setAs for the Subclass "POSIXt"
setAs("POSIXt", "dtime", def = function(from) {
  new("dtime", data = from)
})

## Now the transformation to "dtime" class works
as(y2, "dtime")

## I am using:
> version
platform i686-pc-linux-gnu
arch     i686             
os       linux-gnu        
system   i686, linux-gnu  
status   alpha            
major    2                
minor    1.0              
year     2005             
month    03               
day      24               
language R



More information about the R-devel mailing list