Re: [Rd] setClassUnion with numeric; extending class union

From: John Chambers <jmc_at_r-project.org>
Date: Wed, 11 Feb 2009 11:46:54 -0800

What warnings? Which part of the following is not what you're looking for? (The usual information is needed, like version of R, reproducible example, etc.)

 > setClassUnion("numericOrNULL", c("numeric","NULL")) [1] "numericOrNULL"
 > setClass("foo", representation(x="numericOrNULL")) [1] "foo"

 > ff = new("foo", x= 1:10)
 > fg = new("foo", x = NULL)
 >
 > ff

An object of class “foo”
Slot "x":
[1] 1 2 3 4 5 6 7 8 9 10

 > fg
An object of class “foo”
Slot "x":
NULL
 > fk = new("foo")
 > fk
An object of class “foo”
Slot "x":
NULL John

Sklyar, Oleg (London) wrote:
> Dear list:
>
> I am looking for a good way to create an S4 class that would extend
> numeric, but would allow NULL instead of data as well. As far as I can
> see there is no way at the moment to do that, but please correct me if I
> am wrong. The best solution I came up with so far was the following (it
> also indicates a problem of using setClassUnion with numeric as one of
> the classes):
>
> I define a class union of numeric and NULL:
>
> Unfortunately the following works only with warnings:
> setClassUnion("numericOrNULL", c("numeric","NULL"))
>
> So I do a workaround as:
>
> setClass("aNumeric", contains="numeric")
> setClassUnion("numericOrNULL", c("aNumeric","NULL"))
>
> Then I cannot really extend the above virtual class and can only use it
> in a user-defined slot as follows:
>
> setClass("myClass", representation(data="numericOrNULL"))
> new("myClass", data=runif(20))
> new("myClass", data=NULL)
>
> and this works.
>
> Obviously it would be nicer to have something like the following:
>
> setClass("myClass", contains="numericOrNULL")
> new("myClass", runif(20)) ## .Data is not a slot of myClass
> setClass("myClass", representation("numericOrNULL"))
> new("myClass", runif(20)) ## ibid
>
> Technically I understand that the reason behind it failing to work is
> that the virtual class numericOrNULL has not got the .Data slot from
> numeric, but it would be nice to have such a functionality.
>
> Any ideas about better ways for solving such a problem than the one
> described above?
>
> Thanks.
>
> Best,
> Oleg
>
> Dr Oleg Sklyar
> Research Technologist
> AHL / Man Investments Ltd
> +44 (0)20 7144 3107
> osklyar_at_maninvestments.com
>
> **********************************************************************
> Please consider the environment before printing this email or its attachments.
> The contents of this email are for the named addressees ...{{dropped:19}}
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 11 Feb 2009 - 18:53:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 11 Feb 2009 - 22:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive