I define a new function work_with_myS3
that is supposed to work with my new S3 class myS3
:
work_with_myS3 = function (x) {
UseMethod("work_with_myS3", x)
}
work_with_myS3.myS3 = function(x) {
some code
}
When I source this in my normal R session (I'm using RStudio), it behaves exactly as expected. When I feed it an myS3
object, it works; when I feed it something strange, it throws an error:
> work_with_myS3(123)
Error in UseMethod("work_with_myS3", x) :
no applicable method for 'work_with_myS3' applied to an object of class "c('double', 'numeric')"
However, when I include this in my package, build it, reload it and try to call it:
Error: could not find function "work_with_myS3"
Help page works fine though, calling ?work_with_myS3
. This is how I document it by devtools::document()
:
#' Do stuff with myS3
#'
#' What it does
#' @import dplyr
#' @param x object of class myS3
#' @method work_with_myS3 myS3
#' @export
There is also an entry in the namespace:
S3method(work_with_myS3,myS3)
Why is this, and how to make the function available in the package? I suspect I'm making some trivial mistake.
When you call work_with_myS3
with an object of class myS3
the UseMethod
function looks for:
work_with_myS3.myS3
orwork_with_myS3.default
Therefore you need to export work_with_myS3.myS3
, so
#' @export
work_with_myS3.myS3 = function(x) {
## do stuff
}
Alternatively, you could define a default
method and export that.