First, your signature for names.pm is wrong. It should look something more like: names.pm <- function (x) { } As for the body of the function, you might do something like: names.pm <- function (x) { NextMethod() } but you don't need to define a names method if you're just going to call the next method. I would suggest not defining a names method at all. As a side note, I would suggest making your class through the methods package, with methods::setClass("pm", ...) See the documentation for setClass for more details, it's the recommended way to define classes in R. On Wed, Nov 3, 2021 at 2:36 PM Leonard Mada via R-help <r-help at r-project.org> wrote:> Dear List members, > > > Is there a way to access the default names() function? > > > I tried the following: > > # Multi-variable polynomial > > p = data.frame(x=1:3, coeff=1) > > class(p) = c("pm", class(p)); > > > names.pm = function(p) { > # .Primitive("names")(p) # does NOT function > # .Internal("names")(p) # does NOT function > # nms = names.default(p) # does NOT exist > # nms = names.data.frame(p) # does NOT exist > # nms = names(p); # obvious infinite recursion; > nms = names(unclass(p)); > } > > > Alternatively: > > Would it be better to use dimnames.pm instead of names.pm? > > I am not fully aware of the advantages and disadvantages of dimnames vs > names. > > > Sincerely, > > > Leonard > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thank you very much. Indeed, NextMethod() is the correct way and is working fine. There are some alternatives (as pointed out). Although I am still trying to figure out what would be the best design strategy of such a class. Note: - I wanted to exclude the "coeff" column from the returned names; names.pm = function(p) { ?? ?nms = NextMethod(); ?? ?# excludes the Coefficients: ?? ?id = match("coeff", nms); ?? ?return(nms[ - id]); } This is why I also hesitate regarding what to use: dimnames or names. Sincerely, Leonard On 11/3/2021 8:54 PM, Andrew Simmons wrote:> First, your signature for names.pm <http://names.pm> is wrong. It > should look something more like: > > > names.pm <http://names.pm> <- function (x) > { > } > > > As for the body of the function, you might do something like: > > > names.pm <http://names.pm> <- function (x) > { > ? ? NextMethod() > } > > > but you don't need to define a names method if you're just going to > call the next method. I would suggest not defining a names method at all. > > > As a side note, I would suggest making your class through the methods > package, with methods::setClass("pm", ...) > See the documentation for setClass for more details, it's the > recommended way to define classes in R. > > On Wed, Nov 3, 2021 at 2:36 PM Leonard Mada via R-help > <r-help at r-project.org> wrote: > > Dear List members, > > > Is there a way to access the default names() function? > > > I tried the following: > > # Multi-variable polynomial > > p = data.frame(x=1:3, coeff=1) > > class(p) = c("pm", class(p)); > > > names.pm <http://names.pm> = function(p) { > # .Primitive("names")(p) # does NOT function > # .Internal("names")(p) # does NOT function > # nms = names.default(p) # does NOT exist > # nms = names.data.frame(p) # does NOT exist > # nms = names(p); # obvious infinite recursion; > nms = names(unclass(p)); > } > > > Alternatively: > > Would it be better to use dimnames.pm <http://dimnames.pm> instead > of names.pm <http://names.pm>? > > I am not fully aware of the advantages and disadvantages of > dimnames vs > names. > > > Sincerely, > > > Leonard > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On 03/11/2021 2:54 p.m., Andrew Simmons wrote:> First, your signature for names.pm is wrong. It should look something more > like: > > > names.pm <- function (x) > { > } > > > As for the body of the function, you might do something like: > > > names.pm <- function (x) > { > NextMethod() > } > > > but you don't need to define a names method if you're just going to call > the next method. I would suggest not defining a names method at all. > > > As a side note, I would suggest making your class through the methods > package, with methods::setClass("pm", ...) > See the documentation for setClass for more details, it's the recommended > way to define classes in R.That's incorrect. It is *a* recommended way to define classes in R, but there are other recommended ways as well, for doing other kinds of things, and many people stick with the S3 system without formal classes at all. If you're writing a Bioconductor package you should probably use the formal methods. If you're writing code for other purposes, you should think about whether you need formal classes at all, and if so, whether the methods package formalism is a match for what you're doing. Duncan Murdoch