1

I have a generic print function that I think I've set up correctly based on Generic functions (LINK, admittedly a bit hard for me to grasp) and this question(LINK). However, it still throws up a warning in the check. Below is a mock function, print method, roxygen documentation and the error from the check. For background on what the print function is doing; basically I want the output to not look like it is classes but it still carries a class for handling of that object by subsequent functions. How can I make the warning go away (and keep the print function)?

FUN <- function(x) {
    class(x) <- "full_matrix"
    x
}

#' Prints a fuul_matrix object
#' 
#' prints a test object
#' 
#' @param full_matrix The full_matrix object
#' @method print full_matrix
#' @S3method print full_matrix
print.full_matrix <- function(full_matrix) {
    x <- full_matrix
    class(x) <- NULL
    print(x)
}

x <- FUN(mtcars)
x
class(x)

Warning:

* checking S3 generic/method consistency ... WARNING
print:
  function(x, ...)
print.full_matrix:
  function(full_matrix)

print:
  function(x, ...)
print.incomplete_matrix:
  function(incomplete_matrix)

See section 'Generic functions and methods' of the 'Writing R
Extensions' manual.
Community
  • 1
  • 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

1 Answers1

2

From Writing R Extensions

A method must have all the arguments of the generic, including ... if the generic does.

Your method has neither x nor ...

GSee
  • 48,880
  • 13
  • 125
  • 145
  • I didn't understand. Now I do. Can't claim to understand everything but I thought the print method had to have the class name as an argument. It's function name that tells the print method what it is doing. – Tyler Rinker Dec 04 '12 at 02:33
  • 1
    I wonder it you're trying to write `print.full_matrix <- function(x, ...) print(unclass(x), ...)` – GSee Dec 04 '12 at 02:42
  • you don't need the `#' @param` line – GSee Dec 04 '12 at 02:44
  • you're correct on the `print(unclass(x), ...)` but removing the `#' @param` gives a warning – Tyler Rinker Dec 04 '12 at 02:57
  • 2
    Oh right, you need `@param`... unless you use `#' @ keywords internal` instead. Also from Writing R Extensions: "The special keyword ‘internal’ marks a page of internal objects that are not part of the package's API. If the help page for object foo has keyword ‘internal’, then help(foo) gives this help page, but foo is excluded from several object indices, including the alphabetical list of objects in the HTML help system." – GSee Dec 04 '12 at 03:10
  • I suspect you don't need any roxygen commands apart from `@S3method print full_matrix` which will ensure the method is exported. Generally you don't need to document small methods like this. – hadley Dec 04 '12 at 15:32
  • @GSee `print.default(x)` is better than `print(unclass(x))` for large objects, since it avoids making a copy of `x`. – hadley Dec 04 '12 at 15:33
  • @hadley, I actually made the same comment, but deleted it because I think that does something different. `print(unclass(x))`, (or setting `class(x) <- NULL`, then `print(x)`) does not necessarily cause `print.default` to be dispatched. It seems clear that the object is a matrix in this case, so it is very likely that `print.default` is what the OP wants, but there could be a `print.numeric`, `print.matrix`, etc. method defined that would be dispatched instead of `print.default`. – GSee Dec 04 '12 at 15:51
  • @GSee agreed - but it is rare for base R functions to have methods for you atomic vector types, and I did check for `print`. – hadley Dec 04 '12 at 20:50
  • @hadley, I know I'm being nit-picky here, but the methods for atomic vector types don't have to be defined in base R. They could be defined in the OP's package; although, I'm sure doing that is strongly discouraged. (see an example at http://stackoverflow.com/a/13574273) – GSee Dec 04 '12 at 22:26
  • Actually, that leads to interesting behavior. After attaching such a package, `matrix(1:5)` uses `print.default`, but `base::print(matrix(1:5))` uses the `print.matrix` method defined in the package – GSee Dec 04 '12 at 22:47
  • @GSee regardless of what method actually gets called, you're best of figuring that out once and using it - that's my main point. – hadley Dec 05 '12 at 13:43