[R-pkg-devel] Exporting S3 methods for base generics

Joris Meys Joris.Meys at ugent.be
Mon Jun 26 19:10:53 CEST 2017


Hi Charles,

if a generic exists already in the base, you only have to export the actual
S3 method. Your problem is that base::pmax() is not a generic S3 function.
So R gives you the correct warning: the S3 generic in your package will
always mask the base pmax function. And that's not really a problem,
especially since you ensured the base functionality with your default
method.

If you want to avoid that warning, use S4.

#' @rdname
setGeneric("pmax")

#' @rdname pmax
#' @method pmax myclass
#' @export
setMethod("pmax",
                  "myclass",
                  function(...){
                     # do some stuff
})

More information on how to deal with dots can be found on the help page
?dotsMethods.

If you have a generic in the base package (eg plot is such one), you only
define the method and use:

#' @export
plot.myclass <- function(x, y, ...){
   # do some more stuff
}

Cheers
Joris



On Mon, Jun 26, 2017 at 6:28 PM, Charles Determan <cdetermanjr at gmail.com>
wrote:

> Greetings R users,
>
> I was wondering how others are exporting S3 methods in their packages when
> the generic is in 'base'.  For example, let's say I want to export a new
> pmax method.  The only way I have found to get this to work is by
> redefining the function with 'UseMethod' and setting the default method.
>
> #' @export
> pmax <- function(...){ UseMethod("pmax") }
> #' @export
> pmax.default <- function(..., na.rm=FALSE){ base::pmax(..., na.rm=FALSE) }
>
> setClass("myclass")
>
> #' @export
> pmax.myclass <- function(..., na.rm = FALSE){
>     print('myclass pmax!')
> }
>
> Although this works, I get the 'warning'
>
> The following objects are masked from 'package:base':
>
>     pmax
>
>
> I would like the package build and loading to be as clean as possible but
> if this is acceptable and not considered a problem I will let it go.  It
> just seems odd that one would to redefine a the generic when in states in
> the docs for 'pmax' that it will also work on classed S3 objects but
> perhaps I am reading this incorrectly.
>
> Thanks,
> Charles
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-package-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Mathematical Modelling, Statistics and Bio-Informatics

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list