4

This question is related to but distinct from Exporting non-S3-methods with dots in the name using roxygen2 v4. From that post I learned that one needs to use @export function.name in order for the NAMESPACE to be written correctly by roxygen. I have done that and the NAMESPACE is correctly written.

My problem comes when I do R CMD Check. I have legacy code that has a function tail.g(). R CMD Check throws a NOTE noting that an apparent S3 method has been exported but not registered.

A reproducible example is below. Observe that xxxx.g doesn't have the NOTE, which leads me to believe that because tail is a generic in the utils package I need some special work-around. I'd prefer not to rename tail.g to tail_g because this is legacy code. I wish to eliminate all notes for a successful CRAN submission.

library(roxygen2)
package.skeleton("test")
writeLines(
  "#' Check an argument 
  #' 
  #' Checks an argument.
  #' @param ... Some arguments.
  #' @return A value.
  #' @export tail.g
  tail.g <- function(...) 0",
  "test/R/tail.g.R"
)
writeLines(
  "#' Check an argument 
  #' 
  #' Checks an argument.
  #' @param ... Some arguments.
  #' @return A value.
  #' @export xxxx.g
  xxxx.g <- function(...) 0",
  "test/R/xxxx.g.R"
)
roxygenise("test")
setwd("./test")
devtools::check(document=FALSE)

Gives the NOTE:

checking S3 generic/method consistency ... NOTE
Found the following apparent S3 methods exported but not registered:
  tail.g

How do I eliminate the NOTE for tail.g() without renaming?

Calimo
  • 7,510
  • 4
  • 39
  • 61
swihart
  • 2,648
  • 2
  • 18
  • 42

1 Answers1

1

This is a dirty hack, but it works: just register the functions as methods, in addition to the export.

So your NAMESPACE could have two lines like this:

export(tail.g)
S3method(tail, g)

This appears to be enough to eliminate the warning and get a clean CRAN submission. The only negative side effect I can think of for regular users is a few bogus entries in the output of things like methods(class="g") or methods("tail"). Most regular users shouldn't even notice.

With roxygen2, you can use the @rawNamespace tag to include the required S3method directive in your NAMESPACE file:

writeLines(
  "#' Check an argument 
  #' 
  #' Checks an argument.
  #' @param ... Some arguments.
  #' @return A value.
  #' @export tail.g
  #' @rawNamespace S3method(tail, g)
  tail.g <- function(...) 0",
  "test/R/tail.g.R"
)
Calimo
  • 7,510
  • 4
  • 39
  • 61
  • Unfortunately, it will still throw an error if the signatures are incompatible, e.g. if your function is `tail.g(foo, bar)` and the existing method is `tail(x, ...)`. – Ken Williams Mar 15 '22 at 22:02