I've read several SO answers and still not able to piece together how S3 methods need to be documented when they have different arguments. In the example below, I want the user to be able to enter two objects or pipe in a list. The code works fine. It just spits out a warning about method consistency. I've tried moving the list
param around but always get some combination of the warning of either foo
!= foo.list
or foo
!= foo.default
. There is a dummy reprex in the collapsed code section and relevant links at the end. Thanks!
#' does some stuff
#' @param list for list methods
#' @param x arg1
#' @param y arg2
#' @param ... some argument
#' @export
foo <- function(list, x, y, ...) UseMethod('foo')
#' @export
foo.default <- function(x, y, ...) paste(x, y, ...)
#' do stuff for lists
#' @export
#' @param list for list methods
#' @inheritParams foo
foo.list <- function(list, x, y, ...) foo(list[[x]], list[[y]])
# create dummy package
tmp <- tempdir()
setwd(tmp)
devtools::create(path = "test")
setwd("test")
usethis::use_mit_license()
# add R code
writeLines(
con = "R/test.R",
text =
"#' does some stuff
#' @param list for list methods
#' @param x arg1
#' @param y arg2
#' @param ... some argument
#' @export
foo <- function(list, x, y, ...) UseMethod('foo')
#' @export
foo.default <- function(x, y, ...) paste(x, y, ...)
#' do stuff for lists
#' @export
#' @param list for list methods
#' @inheritParams foo
foo.list <- function(list, x, y, ...) foo(list[[x]], list[[y]])"
)
devtools::document()
devtools::load_all()
# examples
foo(1, 2)
list(a = 1, b = 2) |> foo("a", "b")
# check
devtools::check(document = FALSE)
W checking S3 generic/method consistency (561ms)
foo:
function(list, x, y, ...)
foo.default:
function(x, y, ...)
See section 'Generic functions and methods' in the 'Writing R
Extensions' manual.
Here are some related SO posts: