Renaud Gaujoux
2011-Mar-04 11:30 UTC
[Rd] Extending type list: names and inherited methods issue
Hi, I want to extend the type list, but it looks like the names are not handled properly (in the show method), not the [ method. See below for code example. I imagine this comes from the S3/S4 mixing, but I would like to understand and the recommended work around (that avoid redefining all the list methods [, $, etc...). Thank you. Bests, Renaud # define S4 class that inherits from list setClass('A', contains='list') # nothing to say when one creates an object with an unnamed list x <- new('A', list(1,2,3)) x # set the names: seems ok but they are not printed names(x) <- letters[1:3] names(x) x # same thing if one put the S3 .Data slot names(x at .Data) <- letters[4:6] names(x) x # the subsetting works but returns a list instead of the expected object of class A class(x[1]) > sessionInfo() R version 2.12.1 (2010-12-16) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_ZA.utf8 LC_NUMERIC=C LC_TIME=en_ZA.utf8 LC_COLLATE=en_ZA.utf8 LC_MONETARY=C LC_MESSAGES=en_ZA.utf8 LC_PAPER=en_ZA.utf8 [8] LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_ZA.utf8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base -- Renaud Gaujoux Computational Biology - University of Cape Town South Africa ### UNIVERSITY OF CAPE TOWN This e-mail is subject to the UCT ICT policies and e-mai...{{dropped:5}}
John Chambers
2011-Mar-06 19:07 UTC
[Rd] Extending type list: names and inherited methods issue
The "names" slot is not part of the basic vector types/classes. If you want to extend named lists, extend the class "namedList": > getClass("namedList") Class "namedList" [package "methods"] Slots: Name: .Data names Class: list character Extends: Class "list", from data part Class "vector", by class "list", distance 2 Known Subclasses: "listOfMethods" > setClass("myNamedList", contains = "namedList") [1] "myNamedList" > > mm <- new("myNamedList", list(a=1,b=2)) > mm An object of class "myNamedList" $a [1] 1 $b [1] 2 On 3/4/11 3:30 AM, Renaud Gaujoux wrote:> Hi, > > I want to extend the type list, but it looks like the names are not > handled properly (in the show method), not the [ method. See below for > code example. > I imagine this comes from the S3/S4 mixing, but I would like to > understand and the recommended work around (that avoid redefining all > the list methods [, $, etc...). > Thank you. > > Bests, > Renaud > > # define S4 class that inherits from list > setClass('A', contains='list') > > # nothing to say when one creates an object with an unnamed list > x <- new('A', list(1,2,3)) > x > > # set the names: seems ok but they are not printed > names(x) <- letters[1:3] > names(x) > x > # same thing if one put the S3 .Data slot > names(x at .Data) <- letters[4:6] > names(x) > x > > # the subsetting works but returns a list instead of the expected object > of class A > class(x[1]) > > > > sessionInfo() > R version 2.12.1 (2010-12-16) > Platform: x86_64-pc-linux-gnu (64-bit) > > locale: > [1] LC_CTYPE=en_ZA.utf8 LC_NUMERIC=C LC_TIME=en_ZA.utf8 > LC_COLLATE=en_ZA.utf8 LC_MONETARY=C LC_MESSAGES=en_ZA.utf8 > LC_PAPER=en_ZA.utf8 > [8] LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_ZA.utf8 > LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base >