r-lib/pkgload

pkgload with internal methods

Closed this issue · 2 comments

Trying to debug my package and it seems that internal generics and methods aren't loaded by load_all() as I would expect them to. This file is in my R/ directory:

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

bar.numeric <- function(x,...){
  cat("This is a numeric object.\n")
}

bar.default <- function(x,...){
  cat("This is an unknown obect.\n")
}

#'@export
foo <- function(x){
  bar(x)
}

And here's my console:

> load_all()
Loading testpkg
> bar(1)
Error in UseMethod("bar", x) : 
  no applicable method for 'bar' applied to an object of class "c('double', 'numeric')"
> bar("string")
Error in UseMethod("bar", x) : 
  no applicable method for 'bar' applied to an object of class "character"
> foo(1)
This is a numeric object.
> foo("string")
This is an unknown obect.

It still gives the same errors if I export only bar() and if I use export_all=T. The only fix would be to export the bar.*() methods, which I don't want to do in my package (since they still work fine inside other functions and my intent is to only ever use them internally). Debugging with sloop implies these functions can be found.

> sloop::s3_dispatch(bar(1))
   bar.double
=> bar.numeric
 * bar.default

They are in the package environment in the environment pane, and I can call the method specifically since they are being loaded:

> bar.numeric(1)
This is a numeric object.

They just don't seem to be being registered as S3methods (and aren't in the NAMESPACE file).

What would be the easiest way to debug these kind of functions without exporting them? Is this an Issue with pkgload? Or is it something out-of-scope?

You still need to add #' @export to the S3 methods, even if the generic is not exported.

Ahh okay. Thanks 👍