0

I have a package where I have a couple of generic methods. They're all S3 generics with the structure typical of what you'd see in one:

func_one <- function(x, ...) { UseMethod("func_one") } 
func_one.data.frame(function(x,...) { dim(x) } 
func_one.matrix(function(x, ...) {dim(x) } 

In my NAMESPACE file, I have tried exporting only the top function(func_one), but then when I would go to run func_one on a data.frame, or a matrix, it would error out. I then went ahead and added func_one.data.frame AND func_one.matrix in the NAMESPACE, but this still didn't do anything.

Just to be clear, these generic methods would FIND in R, but just not as a package. Any help would be great. :)

Here's the namespace file:

S3method(func_one,data.frame)
S3method(func_one,matrix)
S3method(func_one,tbl_df)
export(func_one)

Here's an example of the actual function file (func.R)

 #' @export
    func_one <- function(data, ...) {
      UseMethod("func_one")
    }

 #' @export
        func_one.data.frame <- function(data, ...) {
            dim(x) 
        }

 #' @export
        func_one.matrix <- function(data, ...) {
            dim(x) 
        }

Then after I install the R package within R, and try using it, I get a UseMethod error.

Steveman30290
  • 511
  • 7
  • 17
  • More details here: https://stackoverflow.com/questions/18512528/how-to-export-s3-method-so-it-is-available-in-namespace – MrFlick Aug 23 '20 at 02:54
  • I do have the S3method() for each of the classes that the generic method takes, and then also an export(func_one.). The problem is, the user cannot simply use func_one() and the method will find the right class. Instead, the user has to use func_one.matrix() for a matrix class. Which I feel defeats the point of a generic method. – Steveman30290 Aug 23 '20 at 04:19
  • It would be really useful to see the NAMESPACE file so we can verify and test. And is the code you shared a mistake? Did you define these functions properly? You seem to be calling `func_one.data.frame()` rather than creating it `func_one.data.frame<-function()` – MrFlick Aug 23 '20 at 04:23
  • @MrFlick - I just edited my original post. Hopefully this clarifies things. – Steveman30290 Aug 23 '20 at 04:28
  • Have you built and reloaded the package? Are you sure you are running the most recent version? What message do you get exactly when you run `func_one(data.frame(x=1:3))`? – MrFlick Aug 23 '20 at 04:32
  • I've built and reload the package many, many times and devtools is up to date. Here's the error: `Error in UseMethod("func_one") : no applicable method for 'func_one' applied to an object of class "data.frame"` However, using func_one.data.frame() works. – Steveman30290 Aug 23 '20 at 04:34
  • Run `document()` – Hong Ooi Aug 23 '20 at 04:39
  • When I run the functions above I get `object 'x' not found` because you have `data` in the parameter but `x` in the body. But the generic methods work just fine for me in a test package with that NAMESPACE. If it can find `func_one.data.frame` that means `export(func_one.data.frame)` must be in the NAMESPACE file. Are you absolutely sure you're looking at the right file? Something's not adding up. – MrFlick Aug 23 '20 at 04:43

0 Answers0