Skip to content

[R-pkg-devel] R CHECK warning about new S3 generic/method consistency

7 messages · CRAN.r, Diego Hernangómez Herrero, Duncan Murdoch +1 more

#
I'm trying to define a new generic, and keep getting an S3 generic/method consistency when running R CHECK. All of the code seems to be working, and I'm not getting any note, errors, or other warnings.

This minimal example shows the warning I'm getting. The functions are

  myscale <- function(x, y) UseMethod("myscale")
  myscale.default <- function(x) x

The usage section of the man file is

  \usage{
    myscale(x, y)
    \method{myscale}{default}(x)
  }

and the NAMESPACE file is

  export("myscale", "myscale.default")
  S3method(myscale, default)

When I build the package and run CHECK, I get

  * checking S3 generic/method consistency ... WARNING
  myscale:
    function(x, y)
  myscale.default:
    function(x)
  See section 'Generic functions and methods' in the 'Writing R
  Extensions' manual.

As I understand it, there shouldn't be a problem as long as the generic function contains all possible arguments of any method, and the methods have their arguments in the same order as the generic. It seems that having one method with only "x" shouldn't be a problem. I've read the section mentioned in the warning, but I can't figure out what's going on. Is any of this wrong?

Jay
Message-ID: <vh8JIGJjAnx3iogEj4YOawdfPEiMiPERYzcT-wMwTdjPELDSpPvU8poNFhq69bH1x7Ijnhj07rtShRqhNee5A8-xd-H2PzGTwI5pYBXqCQ4=@proton.me>
#
Shouldn?t you include the y argument also in
myscale.default ? Your generic is defining that argument as well.



Have a nice day!


El El lun, 11 mar 2024 a las 18:25, CRAN.r <cran.r at proton.me> escribi?:

  
  
#
On Monday, March 11th, 2024 at 12:43 PM, Diego Hernang?mez Herrero <diego.hernangomezherrero at gmail.com> wrote:

            
I assume (hopefully correctly) that methods don't need to include all the arguments of the generic. I get the same warning if I use "..." instead of y, too.
Message-ID: <hV4lx_U-zSXxiV1OTTm0J0FGohychf4_b0vU90KlE0JwYYiK2TZTya9uRLOdsfj9CUDjX4qYp7oF8Nbqwn62M-rFtWC23Ds_eVA_De_ls5g=@proton.me>
#
Usually when I write a generic I use ? in the definition to allow using
additional arguments in specific methods, see

as_bibentry <- function(x, ...) { UseMethod("as_bibentry")
 }

as_bibentry.cff <- function(x, ..., what = c("preferred", "references",
"all")) {

<code>
}

but in any case the method must include at least the arguments defined in
the same order. Using ? in the generic allows me to include `what` arg with
no WARNINGs.




Have a nice day!


El El lun, 11 mar 2024 a las 19:13, CRAN.r <cran.r at proton.me> escribi?:

  
  
#
On 11/03/2024 2:13 p.m., CRAN.r wrote:
No, your assumption is backwards.  The methods do need to include all 
arguments of the generic.  As Writing R Extensions says near the start 
of section 7, "A method must have all the arguments of the generic, 
including ? if the generic does."

Think about your user.  They'll ask about help for `inmyscale`, and see 
that it has two arguments, x and y.  If x is a type that goes to 
`inmyscale.default`, the user would receive an error when they followed 
the docs and included the y value.

The usual way to handle this is to include both x and y in all methods, 
but document some of them to say that y is ignored.

Duncan Murdoch
#
That's embarrassing. I was worried it was something simple I missed. Thanks for pointing that out!
Message-ID: <fVt-tX5u6EjRDSCP4AZv3yWNGgzporPZA1ycaDm-6cp2jAnaBFJjxIBPHKbI9P-wWaVnoeblmujmhjfhN1s8lC2IG5OwhjKH84h3ZvSE6kg=@proton.me>
#
On 11.03.2024 19:34, CRAN.r wrote:
Even more embarassing given you seem to be Mr CRAN given your mail 
message's "From" field ...

Uwe Ligges