2013-03-31 47 views
4

我試圖創建一個自動化的R函數,該函數創建一個乳膠文件,該文件可以由外部Latex程序運行(不需要進一步修改Latex代碼)。我知道KnitR和Sweave,但我需要更簡單的東西。基本上對可我想在末尾添加修改後的xtable函數的獨立乳膠表

%A1在頂部

\documentclass{article} 
\pagestyle{empty} 
\begin{document} 

%A2被打印出來通過xtable任何對象

\end{document} 

這意味着,我可以只運行我的分析和(當我需要時)創建一個可以被基於Latex的程序操縱的外部文件。我不希望將所有表格放在一個文檔中,但我希望它們分開(因此需要\ begin {document}和\ end {document}。我一直在嘗試使用add.to.rows(xtable函數)但我還是一無所獲

例:

data(tli) 
tli.table <- xtable(tli[1:10,]) 
digits(tli.table)[c(2,6)] <- 0 
print(tli.table,floating=FALSE) 

你會如何添加A1和A2,這樣的結果是:

\documentclass{article} 
\pagestyle{empty} 
\begin{document} 
\begin{table}[ht] 
\centering 
\begin{tabular}{rrlllr} 
    \hline 
& grade & sex & disadvg & ethnicty & tlimth \\ 
    \hline 
1 & 6 & M & YES & HISPANIC & 43 \\ 
    2 & 7 & M & NO & BLACK & 88 \\ 
    3 & 5 & F & YES & HISPANIC & 34 \\ 
    4 & 3 & M & YES & HISPANIC & 65 \\ 
    5 & 8 & M & YES & WHITE & 75 \\ 
    6 & 5 & M & NO & BLACK & 74 \\ 
    7 & 8 & F & YES & HISPANIC & 72 \\ 
    8 & 4 & M & YES & BLACK & 79 \\ 
    9 & 6 & M & NO & WHITE & 88 \\ 
    10 & 7 & M & YES & HISPANIC & 87 \\ 
    \hline 
\end{tabular} 
\end{table} 
\end{document} 

我被困在一開始:

bottom <- list();bottom$pos<- list() 
bottom$pos[[1]] <- c(nrow(x)) 
bottom$command <-c("\\end{document} \n") 
print(xtable(tli[1:10,]),add.to.row=bottom) 

我需要\ {結束}文件是正確的結束,但如果我改變

bottom$pos

bottom$pos[[1]] <- c(nrow(x)+3)

我得到一個錯誤。我也沒有包括頂部(A1-見上文)。

理想情況下,我希望代碼儘可能通用,以便它可以應用於任何xtable輸出(例如anovas,sideway表等)。

任何想法?

非常感謝

+0

你不能只用'\ input'與前導材料的虛擬文件? –

回答

5

catprint.xtable功能都有一個「追加」的說法:

wrapLatex <- function(dat,fil, ...) { 
    cat(c("\\documentclass{article}\n", 
      "\\pagestyle{empty}\n", 
      "\\begin{document}\n"), file=fil) 
    print(dat, file=fil, append=TRUE, ...) 
     cat("\\end{document}\n", file=fil, append=TRUE) } 
wrapLatex(tli.table, fil="~/test") 
+0

我不能相信我忘了這張貼。我也顯然忘了將它添加到我的.Rprofile文件中。我在過去的3年裏曾多次要這麼做。 –