[Rd] Make as.factor an S3 generic?

Martin Morgan mtmorgan at fhcrc.org
Wed Apr 20 20:40:31 CEST 2011


On 04/20/2011 11:38 AM, Prof Brian Ripley wrote:
> Well, lots of functions are not generic. We do ask you to give a case
> for such changes ... where is it?

The specific need started with base::lapply, which calls base::as.list. 
An S4 method "as.list,A-method" defined in a name space isn't seen by 
base::as.list, whereas as.list.A is (see discussion in ?Methods around 
/f3.myClass). The question is from this post on a Bioconductor mailing list

https://stat.ethz.ch/pipermail/bioc-sig-sequencing/2011-April/001979.html

[partly answering Bill's question here] A list() constructor could be 
tricky to implement (dealing with variable numbers of arguments and the 
S4 rules for dispatch on ...), whereas as.list.A is trivial (slot 
extraction, in my case). Having arrived at an easy solution, I marched 
through the other coercion functions with only minor set-backs 
(as.double.A instead of as.numeric.A) until factor.

Martin

>
> On Wed, 20 Apr 2011, Martin Morgan wrote:
>
>> as.factor / as.ordered is not written as a generic. This differs from
>> as.numeric, as.matrix, and other as.*. The following seems to address
>> this and does not break make check-all.
>>
>> FWIW, the patch is against r55563, because with r55564 I see
>
> OS-specific ....
>
>> /home/mtmorgan/src/R-devel/src/main/dounzip.c:75:15: error: storage
>> size of ‘dt’ isn’t known
>> /home/mtmorgan/src/R-devel/src/main/dounzip.c:88:5: warning: implicit
>> declaration of function ‘mktime’
>> make[3]: *** [dounzip.o] Error 1
>> make[3]: *** Waiting for unfinished jobs....
>> make[3]: Leaving directory `/home/mtmorgan/bin/R-devel/src/main'
>> make[2]: *** [R] Error 2
>> make[2]: Leaving directory `/home/mtmorgan/bin/R-devel/src/main'
>> make[1]: *** [R] Error 1
>> make[1]: Leaving directory `/home/mtmorgan/bin/R-devel/src'
>> make: *** [R] Error 1
>>
>>
>> Index: src/library/base/R/factor.R
>> ===================================================================
>> --- src/library/base/R/factor.R (revision 55563)
>> +++ src/library/base/R/factor.R (working copy)
>> @@ -45,7 +45,9 @@
>> }
>>
>> is.factor <- function(x) inherits(x, "factor")
>> -as.factor <- function(x) if (is.factor(x)) x else factor(x)
>> +as.factor.default <- function(x, ...)
>> + if (is.factor(x)) x else factor(x, ...)
>> +as.factor <- function(x, ...) UseMethod("as.factor")
>>
>> ## Help old S users:
>> category <- function(...) .Defunct()
>> @@ -245,7 +247,10 @@
>> ordered <- function(x, ...) factor(x, ..., ordered=TRUE)
>>
>> is.ordered <- function(x) inherits(x, "ordered")
>> -as.ordered <- function(x) if(is.ordered(x)) x else ordered(x)
>> +as.ordered.default <- function(x, ...)
>> + if(is.ordered(x)) x else ordered(x, ...)
>> +as.ordered <- function(x, ...)
>> + UseMethod("as.ordered")
>>
>> Ops.ordered <- function (e1, e2)
>> {
>> Index: src/library/base/man/factor.Rd
>> ===================================================================
>> --- src/library/base/man/factor.Rd (revision 55563)
>> +++ src/library/base/man/factor.Rd (working copy)
>> @@ -10,7 +10,9 @@
>> \alias{is.factor}
>> \alias{is.ordered}
>> \alias{as.factor}
>> +\alias{as.factor.default}
>> \alias{as.ordered}
>> +\alias{as.ordered.default}
>> \alias{is.na<-.factor}
>> \alias{Math.factor}
>> \alias{Ops.factor}
>> @@ -40,8 +42,8 @@
>> is.factor(x)
>> is.ordered(x)
>>
>> -as.factor(x)
>> -as.ordered(x)
>> +as.factor(x, \dots)
>> +as.ordered(x, \dots)
>>
>> addNA(x, ifany=FALSE)
>> }
>>
>> --
>> Computational Biology
>> Fred Hutchinson Cancer Research Center
>> 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109
>>
>> Location: M1-B861
>> Telephone: 206 667-2793
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793



More information about the R-devel mailing list