title: Print methods in Roxygen2
body: <p>I am trying to get familiar with the package <code>roxygen2</code>.
  In particular, I want to understand how print summary methods work in <code>roxygen2</code>.
  Suppose that I need to create a function of the kind:</p>
  
  <pre><code>#' my function
  #' @param mat an object of class \code{matrix} with 4 rows and 4 columns
  #' @return an object of class \code{matrix} with 4 rows and 4 columns
  #' @export
  my_function &lt;- function(mat){
    res &lt;- list()
    res[["output.1"]] &lt;- mat
    class(res) &lt;- "mynewpackage"
    attributes(res[["output.1"]])$attr &lt;- "output.a"
    res[["output.2"]] &lt;- mat
    class(res) &lt;- "mynewpackage"
    attributes(res[["output.2"]])$attr &lt;- "output.a"
    res[["output.3"]] &lt;- mat
    class(res) &lt;- "mynewpackage"
    attributes(res[["output.3"]])$attr &lt;- "output.b"
    return(res)
  }
  </code></pre>
  
  <p>Then I want to create a <code>summary</code> for my function. I do:</p>
  
  <pre><code>#' summary
  #' @title summary
  #' @method summary mynewpackage
  #' @noRd
  #' @export
  "summary.mynewpackage" &lt;- function(object, print = "output.1", ...) {
  
    if (print == "output.1") {
      res &lt;- list(coefficients = object[["output.1"]],
                  print = print)
      print(res)
    } else if (print == "output.2") {
      res &lt;- list(coefficients = object[["output.2"]],
                  print = print)
      print(res)
    } else if (print == "output.3") {
      res &lt;- list(coefficients = object[["output.3"]],
                  print = print)
      print(res)
    }
  
    class(res) &lt;- "summary.mynewpackage"
    res
  }
  </code></pre>
  
  <p>Finally, I want my the output of my summary to look more like<code>print.summary.lm</code>. 
  To this purpose, and this is where I get lost, I tried, unsuccesfully, something like:</p>
  
  <pre><code>#' print
  #' @title print
  #' @aliases print.mynewpackage
  #' @param x numeric number
  #' @param digits number of digits
  #' @param ... other arguments
  #' @noRd
  #' @export
  "print.mynewpackage" &lt;- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
  
    x &lt;- x[[sample(1:3, 1)]]
  
    if( attributes(x[["output.1"]])$attr == "output.a" ) {
  
      print.default(format(x[,1], digits = digits), print.gap = 2L,
                    quote = FALSE)
      cat("\n")
      invisible(x)
    } else {
  
      print.default(format(x[,1], digits = digits), print.gap = 2L,
      quote = FALSE)
      cat("\n")
      invisible(x)
    }
  
  }
  #' @rdname print
  #' @param x numeric number
  #' @param ... other arguments
  #' @method print summary
  #' @noRd
  #' @export
  "print.summary" &lt;- function(x, ...){
    UseMethod("print.summary")
  }
  #' @rdname print
  #' @title print
  #' @name print
  #' @aliases print.summary
  #' @param object numeric number
  #' @param ... additional parameters
  #' @param print object to be printed
  #' @param digits number of digits to be printed
  #' @param signif.stars TRUE or FALSE
  #' @method print.summary mynewpackage
  #' @importFrom stats printCoefmat
  #' @export
  "print.summary.mynewpackage" &lt;- function(object, print = "output.1", digits = max(3L, getOption("digits") - 3L), signif.stars = getOption("show.signif.stars"), ...) {
  
    if (print == "output.1") {
      printCoefmat(object[["output.1"]], digits = digits, signif.stars = signif.stars)
    } else if (print == "output.2") {
      printCoefmat(object[["output.2"]], digits = digits, signif.stars = signif.stars)
    } else if (print == "output.3") {
      printCoefmat(object[["output.3"]], digits = digits, signif.stars = signif.stars)
    }
  }
  </code></pre>
  
  <p>What I was expecting was to run</p>
  
  <pre><code>cmat &lt;- cbind(rnorm(3, 10), sqrt(rchisq(3, 12)))
  cmat &lt;- cbind(cmat, cmat[, 1]/cmat[, 2])
  cmat &lt;- cbind(cmat, 2*pnorm(-cmat[, 3]))
  colnames(cmat) &lt;- c("Estimate", "Std.Err", "Z value", "Pr(&gt;z)")
  
  my_function(cmat)
  summary(my_function(cmat))
  </code></pre>
  
  <p>And obtain for <code>summary(my_function(cmat))</code> the output produced by <code>print.summary.mynewpackage(my_function(cmat))</code></p>
  
  <p>However, the only way to access to <code>print.summary.mynewpackage()</code> is to run:</p>
  
  <pre><code>mynewpackage:::print.my_new_function(my_function(cmat))
  </code></pre>
  
  <p>Any hint on how should I do this?</p>