Skip to content
Prev 10835 / 12125 Next

[R-pkg-devel] A function in one of my package is now a method in base R

On 2024-08-02 10:35 a.m., Shu Fai Cheung wrote:
I think it's impossible to avoid some inconvenience to your users.

Here's what I'd suggest:

- Create a method for base::sort_by(), as you suggested you could.  Use 
the generic's variable names for compatibility, but also add your extra 
variable names as additional arguments, e.g.

  sort_by.est_table <- function(x, y, object,
    by = c("op", "lhs", "rhs"),
    op_priority = c("=~", "~", "~~", ":=", "~1", "|", "~*~"),
    number_rows = TRUE, ...) {

   # This test seems unlikely:  how would we have dispatch here if we 
specified object explicitly?

   if (!missing(object) {
     if (!missing(x))
       stop("both x and object specified!")
     x <- object
   }

   # This one is more likely to do something:

   if (!missing(by)) {
     if (!missing(y))
       stop("both y and by specified!")
     y <- by
   }

   # Now proceed using x and y

   ...
}

- Create a separate function, e.g. sort_by_old() which is exactly 
compatible with your old sort_by().  For users where the above doesn't 
just work, they can switch to sort_by_old().

Duncan Murdoch