简体   繁体   中英

R hierarchy and passing arguments to correct function

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). Eg:

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM