Skip to content
Prev 42328 / 63424 Next

factor S4 class is NA when as.character method exists

On 1/24/12 9:35 AM, Prof Brian Ripley wrote:
Creating a simple generic version of unique() (not just "some other 
function...") causes S4 method selection to work for code that has 
access to that function, but calls from within the base namespace will 
still see the S3 version.

The safest technique is to ensure that both S4 and S3 dispatch see the 
same method.
------------------------
setClass("myFactor", contains = "factor")

setGeneric("unique")

unique.myFactor <- function (x, incomparables = FALSE, ...)
     unique(as.character(x))

setMethod("unique", "myFactor", unique.myFactor)
------------------------

With this in PkgA and suitable exports from the namespace:
[1] unique.POSIXlt         unique.array           unique.data.frame
[4] unique.default         unique.matrix          unique.myFactor
[7] unique.numeric_version
Function: unique (package base)
x="ANY"
x="myFactor"

Someday there may be a more natural approach.

John