Take the function names
: that's a primitve function in R. For primitive functions, an implicit S4 generic is created, so it is possible to construct S4 methods for that function.
Take an S4 class defined as follows :
setClass("aClass",
representation=list(
values = "character",
id = "numeric"
),
prototype=list(
values = character(0),
id = numeric(0)),
validity=function(object){
length(object@values)==length(object@id)
}
)
Now I want to create a function to extract the names, either sorted or unsorted. I wanted to do this using the function names
to avoid having to make a new function getNames()
or whatever, as that's less intuitive.
The following gives an idea of what needs to be done:
setMethod("names",signature="aClass",
function(x,ordered=TRUE){
if(ordered)
x@values[x@id]
else
x@values
}
This won't work, as names
is a primitive function and ordered
is not an argument for the implicit generic.
How can I make this work under the following conditions:
- the
names
function should keep its original behaviour for all other objects, including objects from other packages. - the code should be acceptable for use in a package
- the code should be acceptable by the high standards set by eg Bioconductor.