Thanks for the reply Joris, although I am not sure what I could be doing
wrong. I implement exactly the lines you show and yet I just get the
following error when I call 'pmax' on the class.
Error in mmm < each :
comparison (3) is possible only for atomic and list types
In addition: Warning message:
In is.na(mmm) : is.na() applied to non-(list or vector) of type 'S4'
Regards,
Charles
On Mon, Jun 26, 2017 at 12:10 PM, Joris Meys <Joris.Meys at ugent.be> wrote:
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]]