5

I have already read following two discussion:
Roxygen2 - how to properly document S3 methods
S3 method consistency warning when building R package with Roxygen
And following two tutorial:
http://cran.r-project.org/doc/manuals/R-exts.html#Generic-functions-and-methods
http://adv-r.had.co.nz/S3.html,
but my problem is still not solved. Here are details:
I want to define a S3 method of plot() generic in a package, my code is:

#' description
#'
#' more details
#'
#' @param x "test" object
#' @param label parameter one
#' @param log parameter two
#' @param ... graphical parameters to plot
#'
#' @examples
#' plot(a)
#'
#' @export
plot <- function(x, label=TRUE, log=TRUE,  ...){
  UseMethod("plot")
}

#' @export
plot.test <- function(x, label=TRUE, log=TRUE, ...){
# some code
}

After running devtools::check(), I will get following warning:

checking S3 generic/method consistency ... WARNING
plot:
  function(x, log, ...)
plot.test:
  function(x, label, log, ...)

See section ‘Generic functions and methods’ in the ‘Writing R
Extensions’ manual.

Look like the parameter label disappear, I tried exchange the position of parameter log and label, any parameter after x will disappear in line function(x, log, ...), so how to fix this?

David Lee
  • 129
  • 1
  • 9

2 Answers2

2

Are you using plot() generic function as an example or it's actually your code?

If latter is the case, I think it's because plot() is a base R function that takes three args: x, y, and ... . To let your own s3 method pass the check, there is no need to redefine your own generic, and also your s3 method will have to follow the exact same arguments with the base R generic.

fhlgood
  • 479
  • 4
  • 9
  • Thank you for pointing this out. the `plot()` generic function is the actual function I want to add a method to. So do you mean I cannot add any new parameter to my function `plot.test()`? – David Lee Mar 07 '18 at 07:15
  • you can, but you should first include all parameters from the generic. In your case: `plot.test <- function(x, y, ..., label = TRUE, log = TRUE) {` or if you want to allow specifying `label` and `log` positionally: `plot.test <- function(x, y, label = TRUE, log = TRUE, ...) {` . Note, however, that even the graphics package doesn't stick to this (in plot.formula) - it seems the plot generic is not as generic as it's use. – jan-glx Nov 18 '20 at 11:22
2

I had the same problem when I run R CMD check or devtools:check() to my package with s3 method, it always gives me warnings because of consistency S3 generic/method.

I also use Roxygen2, and I tried to add the @rdname before the @export to my function with s3 generic and it worked for me. Please try these following code:

##---- You can remove this---###
#plot <- function(x, label=TRUE, log=TRUE,  ...){
#  UseMethod("plot")
#}
#######################################

#' description
#'
#' more details
#'
#' @param x "test" object
#' @param label parameter one
#' @param log parameter two
#' @param ... graphical parameters to plot
#'
#' @examples
#' plot(a)
#' @rdname plot.test
#' @export
plot.test <- function(x, label=TRUE, log=TRUE, ...){
# some code
}

you can remove the first function (plot with UseMethod('plot')).

Let me know whether the warning is gone or not.

Ahmad Zaenal
  • 148
  • 1
  • 7