[R] usage and behavior of 'setIs'

Matthias.Kohl@uni-bayreuth.de Matthias.Kohl at uni-bayreuth.de
Mon Oct 25 11:41:59 CEST 2004


Hello,

am I using 'setIs' in the correct way in the subsequent (artifical) example?

Do I have to specify explicit 'setAs' for 'list' and 'vector' or
should this work automatically, since "getClass("List1")" states
an explicit coerce also for these classes.

I'm working with R 2.0.0 Patched (2004-10-06) on windows 2000.

Thanks for your advice,
Matthias


# example
setClass(Class = "List1", representation(List = "list"))
setClass(Class = "List2", contains = "list")

setIs(class1 = "List1", class2 = "List2",
    coerce = function(obj){ new("List2", obj at List) },
    replace = function(obj, value){
        obj at List <- value
    })

getClass("List1")
# states explicit coerce for 'list' and 'vector'
getClass("List2")
L1 <- new("List1", List = list("a"))

# all TRUE
is(L1, "List2")
is(L1, "list")
is(L1, "vector")

as(L1, "List2") # works

# both return 'list()'
# why not a 'list' with entry "a"?
# Is there an additional 'setAs' needed?
as(L1, "list")
as(L1, "vector")

L2 <- as(L1, "List2")
as(L2, "list") # works
as(L2, "vector") # works




More information about the R-help mailing list