1

I have an S3 class, and I'm trying to work out how to set up a print function for it.

This part's good.

print.webglobe <- function(wg, ...){
  "it worked!"
}

But, if I run devtools::check() on it, I get the following ominous message:

checking S3 generic/method consistency ... WARNING
print:
  function(x, ...)
print.webglobe:
  function(wg, ...)

I tried adding the additional code:

print <- function(wg, ...){
  UseMethod("webglobe", wg)
}

But, with this present, print.webglobe() never seems to be accessed and my S3 class just prints as a list of some sort.

How can I set up this up correctly?

Richard
  • 56,349
  • 34
  • 180
  • 251
  • 2
    Change the `wg` to `x`. The formal arguments of a method have to match those of the generic. That's why the print isn't working the way you would expect. – Thomas Apr 22 '17 at 05:32
  • @Thomas: That was it. Could you please post your comment as an answer instead? – Richard Apr 22 '17 at 08:03

1 Answers1

2

Change the wg to x. The formal arguments of a method have to match those of the generic because arguments from the generic call are passed, based upon name, to the method. That's why the print() isn't working the way you would expect because wg is being sent to the wg rather than the method's first argument.

Thomas
  • 43,637
  • 12
  • 109
  • 140