[Rd] Writing new methods for `names<-`
Martin Morgan
mtmorgan at fhcrc.org
Wed Nov 2 20:21:50 CET 2011
On 11/02/2011 11:57 AM, Max Gasner wrote:
> Hi there,
>
> I'm working with an object whose internal data representation is very
> different from a data.frame (data is stored as a list of big.matrix objects
> from the bigmemory package) but to which I'd like to give users
> data.frame-like access. I've implemented names very easily as follows:
>
> setMethod("names", signature = "my.dataset", definition = function (x) {
> x at colnames
> })
>
> Now I'd like to implement `names<-` analogously.
>
> Of course the following does not work, because only the local copy of the
> dataset is changed:
>
> setMethod("names<-", signature = "my.dataset", definition = function (x,
> value) {
> x at colnames<- value
> })
>
> Following a suggestion I found in the list archives, I thought it might be
> worthwhile to try something like the following:
>
> setMethod("names<-", signature = "my.dataset", definition = function (x,
> value, env = parent.frame()) {
> with(parent.frame(), x at colnames<- value)
> })
>
> But of course because `names<-` is primitive, if I try to redefine the
> generic function as follows so I can pass it another argument:
>
> setGeneric("names<-", def = function (x, value, ...) {
> standardGeneric("names<-") })
>
> Then I get the following error:
>
> Error in setGeneric("names<-", def = function(x, value, ...) { :
> ‘names<-’ is a primitive function; methods can be defined, but the
> generic function is implicit, and cannot be changed.
>
> Any advice on ways around this, or other approaches to the problem, would
> be very welcome!
Hi Max --
'names<-' is already a generic
> getGeneric("names<-")
standardGeneric for "names<-" defined from package "base"
function (x, value)
standardGeneric("names<-", .Primitive("names<-"))
<environment: 0x8f0a48>
Methods may be defined for arguments: x, value
Use showMethods("names<-") for currently available ones.
so write a 'replacement' method
setReplaceMethod("names", "my.dataset", function(x, value) {
x at colnames <- value
x
})
Hope that helps,
Martin
>
> Thanks,
>
> Max
>
>
>
> Max Gasner
> Navia Systems | 415 413 3821
>
> [[alternative HTML version deleted]]
>
>
>
>
> ______________________________________________
> 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