2015-12-20 14 views
2

根據this答案我可以使用include.rownames=FALSE來禁止使用xtable(將r輸出轉換爲LaTeX表的程序包)打印行名稱。例如:R層次結構和傳遞參數來糾正函數

數據:

> 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 

印刷與行名稱:

> 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} 

打印無行名稱:

> 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} 

正如你可以看到include.rownames=FALSE是論據print功能不爲xtable。這怎麼可能?是不是xtable只是將結果打印到標準輸出? print在使用include.rownames=FALSE時如何知道要省略?參考頁面還提到?print.xtable。這是什麼意思?是print.xtable某種特殊的印刷品,或者xtable方法從包print調用或者發生了什麼?

回答

3

功能printR中的通用功能。看看它的定義:

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

就是這樣。 print唯一能做的就是找到適當的函數來傳遞它接收的對象。也就是說,它將根據對象的類將對象分派給另一個函數。

因此,當print收到類xtable的對象時,它使用print.xtable函數,這是xtable對象的打印方法。函數print.xtable來自xtable包,它的寫法正是爲了這個目的,所以它知道如何處理這些對象並理解參數include.rownames

+0

謝謝。你能推薦一些關於這方面的好消息嗎?我發現了這三個網站:http://bit.ly/1hWRxTN http://bit.ly/1OhqHMw http://bit.ly/1Pf0Vpe,但更實際的總結信息從這將是沒有問題的。 –

+0

也許:http://adv-r.had.co.nz/S3.html,http://adv-r.had.co.nz/OO-essentials.html和http://www.dummies.com /how-to/content/how-to-dispatch-to-a-method-in-r.html。 –