r - Add extra arguments to implicit S4 generic for a primitive function -
take function names
: that's primitve function in r. primitive functions, implicit s4 generic created, possible construct s4 methods function.
take s4 class defined 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 want create function extract names, either sorted or unsorted. wanted using function names
avoid having make new function getnames()
or whatever, that's less intuitive.
the following gives idea of needs done:
setmethod("names",signature="aclass", function(x,ordered=true){ if(ordered) x@values[x@id] else x@values }
this won't work, names
primitive function , ordered
not argument implicit generic.
how can make work under following conditions:
- the
names
function should keep original behaviour other objects, including objects other packages. - the code should acceptable use in package
- the code should acceptable high standards set eg bioconductor.
the generic available as
> getgeneric("names") standardgeneric "names" defined package "base" function (x) standardgeneric("names", .primitive("names")) <environment: 0x459c9c0> methods may defined arguments: x use showmethods("names") available ones.
so signature can see short answer can't add arguments. you'd not want create own function names
. hack use package-global variable getoption("pkg_names_ordered")
wouldn't partake of solution myself.
in ways contract set out names
not order (for instance, names , numerical indecies used subset; numerical indices ordered names, or unordered names?), you're proposing new generic anyway.
Comments
Post a Comment