2

According to this answer I can use include.rownames=FALSE to suppress printing row names when using xtable (package to convert r output to LaTeX table). E.g.:

Data:

> head(dataset)
  order original prediction anomaly.score abs.o.p
1     1        0          0             1       0
2     2        0          0             1       0
3     3        0          0             1       0
4     4        0          0             0       0
5     5        0          0             0       0
6     6        0          0             0       0

Printing with row names:

> xtable(head(dataset))
% latex table generated in R 3.1.1 by xtable 1.8-0 package
% Sun Dec 20 18:38:13 2015
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrr}
  \hline
 & order & original & prediction & anomaly.score & abs.o.p \\ 
  \hline
1 &   1 &   0 &   0 & 1.00 &   0 \\ 
  2 &   2 &   0 &   0 & 1.00 &   0 \\ 
  3 &   3 &   0 &   0 & 1.00 &   0 \\ 
  4 &   4 &   0 &   0 & 0.00 &   0 \\ 
  5 &   5 &   0 &   0 & 0.00 &   0 \\ 
  6 &   6 &   0 &   0 & 0.00 &   0 \\ 
   \hline
\end{tabular}
\end{table}

Printing without row names:

> print(xtable(head(dataset)), include.rownames=FALSE)
% latex table generated in R 3.1.1 by xtable 1.8-0 package
% Sun Dec 20 18:49:34 2015
\begin{table}[ht]
\centering
\begin{tabular}{rrrrr}
  \hline
order & original & prediction & anomaly.score & abs.o.p \\ 
  \hline
  1 &   0 &   0 & 1.00 &   0 \\ 
    2 &   0 &   0 & 1.00 &   0 \\ 
    3 &   0 &   0 & 1.00 &   0 \\ 
    4 &   0 &   0 & 0.00 &   0 \\ 
    5 &   0 &   0 & 0.00 &   0 \\ 
    6 &   0 &   0 & 0.00 &   0 \\ 
   \hline
\end{tabular}
\end{table}

As you can see include.rownames=FALSE is argument for print function not for xtable. How is this possible? Isn't xtable just printing the results to standard output? How does print know what to omit when include.rownames=FALSE is used? Referred page also mention ?print.xtable. What does this mean? Is print.xtable some special kind of print or it is xtable method called from package print or what is going on?

Community
  • 1
  • 1
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122

1 Answers1

3

The function print is a generic function in R. Take a look at its definition:

function (x, ...) 
UseMethod("print")

That's it. The only thing print does is to find the appropriate function to pass the object it receives. That is, it is going to dispatch the object to another function according to the class of the object.

So when print receives an object of class xtable, it uses the print.xtable function, which is a print method for xtable objects. The function print.xtable is from the xtable package and it was written exactly for that purpose, so it knows how to handle those objects and understands the argument include.rownames.

Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66
  • Thanks. Can you recommend some good reading about this. I've found those three sites: http://bit.ly/1hWRxTN http://bit.ly/1OhqHMw http://bit.ly/1Pf0Vpe but something more actual that would sum up info from this would be fine. – Wakan Tanka Dec 20 '15 at 22:37
  • Maybe: http://adv-r.had.co.nz/S3.html, http://adv-r.had.co.nz/OO-essentials.html and http://www.dummies.com/how-to/content/how-to-dispatch-to-a-method-in-r.html . – Carlos Cinelli Dec 20 '15 at 23:02