Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking S3 generic/method consistency ... WARNING

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?

like image 808
David Lee Avatar asked Mar 07 '18 05:03

David Lee


2 Answers

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.

like image 181
fhlgood Avatar answered Nov 16 '22 07:11

fhlgood


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.

like image 42
Ahmad Zaenal Avatar answered Nov 16 '22 09:11

Ahmad Zaenal