1

Consider a simple R function:

#' @importFrom BarPackage Bar
Foo <- function (n, x) {
  replicate(1e6, Bar(numeric(n), x))
}

If Bar() is a method, then calling Bar.numeric(numeric(n), x) saves R from having to look up the class of numeric(n); in a real-world example, this shaves about 10% off my run time.

However, Bar.numeric() is a function in a separate package (which I maintain). Roxygen creates export(Bar) and S3method(Bar, numeric) entries in NAMESPACE, but if I add importFrom BarPackage Bar to the FooPackage NAMESPACE and try to run Foo(), I see Warning message: 'Bar.numeric' is not exported by 'namespace:BarPackage' -- as indeed it's not.

It feels like I need to create a separate entry in my NAMESPACE reading export(Bar.numeric), but this feels like a very bad idea (in part because I don't think that Roxygen will do it). Is there any other way of avoiding R having to look up the class of numeric(n) on each call to Bar()?

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Martin Smith
  • 3,687
  • 1
  • 24
  • 51
  • 1
    You could call `yourOtherPackage:::Bar.numeric` directly. Not the wisest to use on someone else's package, but if you're the maintainer, not so bad. – Gregor Thomas Nov 10 '21 at 15:24
  • Good suggestion, but sadly not an option here as the packages are destined for CRAN, where this is not permitted. – Martin Smith Nov 10 '21 at 21:04
  • 1
    Very hacky, but you could rename `Bar.numeric` as, say `Bar_numeric`, and export `Bar_numeric`. Then define `Bar.numeric(Bar, ...) = function(Bar, ...) {Bar_numeric(Bar, ...)}` so the S3 dispatch also works. – Gregor Thomas Nov 10 '21 at 21:32

0 Answers0