Separate latex table with modified xtable function

I am trying to create an automatic R function that creates a latex file that can be run (without any additional corrections to the latex code) using an external Latex program. I know KnitR and Sweave, but I need something a lot easier. Basically, for any object that can be printed via xtable, I want to add

% A1 at the top

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

% A2 at the end

 \end{document} 

This means that I can just start my analysis and (when I need) create an external file that can be manipulated using Latex-based programs. I'm not interested in putting all the tables in one document, but I want them to be separated (hence the need for \ begin {document} and \ end {document}. I experimented with add.to.rows (function xtable) but I won’t go anywhere.

Example:

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

How would you add A1 and A2 so that the result:

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

I am stuck at the very beginning:

 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) 

I need to \ end {document} be at the end, but if I change

bottom$pos

to

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

I get an error message. I also did not include the top (A1- see above).

Ideally, I would like the code to be as general as possible so that it can be applied to any output (e.g. anovas, tables with sidebars, etc.).

Any ideas?

Thank you very much

+4
source share
1 answer

The cat and print.xtable have the argument 'append':

 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") 
+5
source

All Articles