adschai at optonline.net
2007-Aug-13 05:01 UTC
[R] A clean way to initialize class slot of type "numeric" vector
Hi, I have a class definition like this: setClass("foo", representation(members="numeric"), prototype(members=c())) I intend my class to have members, a slot whose value should be a vector of integer. When I initialize this class, I don't have any member yet. So my member is blank. But if I run the above definition into R, it will complain that my slot members is assigned to NULL which does not extend class "numeric". So how can I fix this? Is there any clean way to do this? This is quite a common situation but I can't seem to find a way out. Any help would be really appreciated. Thank you. - adschai
Seth Falcon
2007-Aug-13 05:38 UTC
[R] A clean way to initialize class slot of type "numeric" vector
adschai at optonline.net writes:> Hi, > > I have a class definition like this: > > setClass("foo", representation(members="numeric"), > prototype(members=c())) > > I intend my class to have members, a slot whose value should be a > vector of integer. When I initialize this class, I don't have any > member yet. So my member is blank. But if I run the above definition > into R, it will complain that my slot members is assigned to NULL > which does not extend class "numeric". So how can I fix this? Is there > any clean way to do this? This is quite a common situation but I can't > seem to find a way out. Any help would be really appreciated. Thank > you.How about: setClass("foo", representation(members="numeric"), prototype(members=numeric(0))) That is, use a zero-length vector to signify a vector that isn't there. + seth -- Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center BioC: http://bioconductor.org/ Blog: http://userprimary.net/user/
Prof Brian Ripley
2007-Aug-13 06:22 UTC
[R] A clean way to initialize class slot of type "numeric" vector
Well, c() is NULL, so R did as you asked it to. See ?integer: an integer vector of length 0 can be gotten by integer(0) (and other ways). If you want integers, why have a slot which is numeric?> setClass("foo", representation(members="integer"))[1] "foo"> new("foo")An object of class "foo" Slot "members": integer(0) is the natural and simpler way to do this. On Mon, 13 Aug 2007, adschai at optonline.net wrote:> Hi, > > I have a class definition like this: > > setClass("foo", representation(members="numeric"), > prototype(members=c())) > > I intend my class to have members, a slot whose value should be a vector > of integer. When I initialize this class, I don't have any member yet. > So my member is blank. But if I run the above definition into R, it will > complain that my slot members is assigned to NULL which does not extend > class "numeric". So how can I fix this? Is there any clean way to do > this? This is quite a common situation but I can't seem to find a way > out. Any help would be really appreciated. Thank you. > > - adschai > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595