[Rd] package methods different behaviour R1.9.1 patchedand R2.0.0

John Chambers jmc at research.bell-labs.com
Tue Sep 7 19:41:21 CEST 2004


Wolski wrote:
> 
> Hello!
> 
> A simple example.
> 
> setClass("myclass"
> ,representation(info="character")
> ,contains="matrix"
> )
> 
> setAs("myclass","vector"
> ,def=function(from)
> {
>         print("enters?")
>         to<-summary(from[,1])
>         to<-as.vector(to)
>         to
> }
> )
> 
> #init
> dd<-matrix(1:6,nrow=2)
> rownames(dd)<-c("a","b")
> tt<-new("myclass",dd)
> 
> class(tt)
> as(tt,"vector")
> summary(dd[,1])

Good example.  Thanks.  This was a bug in setAs() that did not set the
coerce method in the case of overriding the default method for a
contained class.  There should be a fix installed in a day or two.



> 
> What I expect.
> R : Copyright 2004, The R Foundation for Statistical Computing
> Version 1.9.1 Patched (2004-08-30), ISBN 3-900051-00-3
> 
> > class(tt)
> [1] "myclass"
> attr(,"package")
> [1] ".GlobalEnv"
> > as(tt,"vector")
> [1] "enters?"
> [1] 1.00 1.25 1.50 1.50 1.75 2.00
> > summary(dd[,1])
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>    1.00    1.25    1.50    1.50    1.75    2.00
> 
> R : Copyright 2004, The R Foundation for Statistical Computing
> Version 2.0.0 Under development (unstable) (2004-09-06), ISBN 3-900051-07-0
> 
> > class(tt)
> [1] "myclass"
> attr(,"package")
> [1] ".GlobalEnv"
> > as(tt,"vector")
> [1] 1 2 3 4 5 6
> > summary(dd[,1])
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>    1.00    1.25    1.50    1.50    1.75    2.00
> 
> Yours
> Eryk
> 
> *********** REPLY SEPARATOR  ***********
> 
> On 9/7/2004 at 10:34 AM John Chambers wrote:
> 
> >>>It's not possible to tell what you're expecting or actually seeing
> >>>from
> >>>this mail.   We need to see your code and the results, not your
> >>>interpretation of how as() is implemented.
> >>>
> >>>Meanwhile, here's how a simple class that extends "matrix" works in
> >>>2.0.0
> >>>
> >>>With the code:
> >>>
> >>> setClass("mat1", representation(id="character"), contains = "matrix")
> >>>
> >>> mm <- matrix(1:12,3,4)
> >>>
> >>> mmm <- new("mat1", mm, id = "numeric")
> >>>
> >>>The result is:
> >>>
> >>>R> as(mmm, "vector")
> >>> [1]  1  2  3  4  5  6  7  8  9 10 11 12
> >>>
> >>>Class "vector" is one of the basic R classes;  as explained in ?as, the
> >>>methods for these use the exisiting as.<class> functions, as.vector in
> >>>this case.
> >>>
> >>>The result is indeed the same as coercing mmm to "matrix" first and then
> >>>to vector
> >>>
> >>>R> as(as(mmm, "matrix"), "vector")
> >>> [1]  1  2  3  4  5  6  7  8  9 10 11 12
> >>>
> >>>as one would expect, but the method for coercing class "mat1" does not
> >>>go through "matrix":
> >>>
> >>>R> selectMethod("coerce", c("mat1", "vector"))
> >>>function (from, to, strict = TRUE)
> >>>{
> >>>    value <- as.vector(from)
> >>>    if (strict)
> >>>        attributes(value) <- NULL
> >>>    value
> >>>}
> >>><environment: namespace:methods>
> >>>
> >>>
> >>>
> >>>Wolski wrote:
> >>>>
> >>>> Hi!
> >>>>
> >>>> Concerning different behaviour between 1.9.1 patched and:
> >>>> R : Copyright 2004, The R Foundation for Statistical Computing
> >>>> Version 2.0.0 Under development (unstable) (2004-09-06), ISBN
> >>>3-900051-07-0
> >>>>
> >>>> Subtitle:
> >>>> as(x,"vector"); x is of class "Massvector"; never enters
> >>>setAs("Massvector","vector"...
> >>>>
> >>>> The same code:
> >>>>
> >>>> as(mvl[[1]],"vector")
> >>>> causes under R1.9.1 to enter the function
> >>>> setAs("Massvector","vector"
> >>>>       ,function(from)
> >>>> ...
> >>>>
> >>>> This never happens under R2.0.0.
> >>>>
> >>>> The class "Massvector" contains class "Matrix"
> >>>>
> >>>> A phenomenological description of what happens instead with R2.0.0 is
> >>>>
> >>>> as(as(mvl[[1]],"Matrix"),"vector")
> >>>>
> >>>> and its not what I would expect.
> >>>>
> >>>> Is this an intended behaviour in R2.0.0?
> >>>>
> >>>> /E
> >>>>
> >>>> ______________________________________________
> >>>> R-devel at stat.math.ethz.ch mailing list
> >>>> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>>
> >>>--
> >>>John M. Chambers
> >>>Bell Labs, Lucent Technologies    office: (908)582-2681
> >>>700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
> >>>Murray Hill, NJ  07974
> 
> Dipl. bio-chem. Witold Eryk Wolski             @         MPI-Moleculare Genetic
> Ihnestrasse 63-73 14195 Berlin                'v'
> tel: 0049-30-83875219                        /   \
> mail: witek96 at users.sourceforge.net        ---W-W----    http://www.molgen.mpg.de/~wolski
>       wolski at molgen.mpg.de

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc



More information about the R-devel mailing list