7

I'm trying on my first attempt on creating an R package. I have some functions that follows below.

#' @export
overview  <- function(x, ...) {
  UseMethod("overview")
} 

overview.query <- function(return.query, ...) {

Now when I use the devtools::load_all() (which loads all functions) everything works, and overview.query is executed when I pass an object of class query.

But rebuilding, and the UseMethod can't find the overview.query function anymore (all functions are thus not loaded), what have I done wrong?

Error message: no applicable method for 'overview' applied to an object of class "c('query', 'data.frame')"

I thought that only functions that are to be exposed to the user are to be @export'ed, and all other functions would still be visible internally to the other package functions.

uncool
  • 2,613
  • 7
  • 26
  • 55
  • possible duplicate: http://stackoverflow.com/questions/32277063/why-doesnt-my-package-function-find-other-non-export-tagged-functions – C8H10N4O2 Jun 02 '16 at 16:45
  • Just for the record, I encountered a version of this while deliberately using unexported S3 generics and methods. When I passed an S3 method to `lapply()`, it worked with `devtools::load_all("drake")` but not `library(drake)`. Solution: nest in another function. https://github.com/ropensci/drake/commit/e5a17410d6b5499682755d187eaaf6030096f590. – landau Feb 13 '19 at 02:36

1 Answers1

5

When you create a generic function to apply to an S3 object, you need to export both the UseMethod statement and the function itself, as in:

#' @export
overview  <- function(x, ...) {
  UseMethod("overview")
} 
#' @export  
overview.query <- function(return.query, ...) {

which ought to eliminate the error as that method is now available to the user.

This is applicable to roxygen2 versions 3+ (currently on 5). See this answer for more info:

How to properly document S3 methods

Community
  • 1
  • 1
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134