I want to fill automaticly data from R into a table for a TeX-document. Sweave and Knitr are possibilities here. Please feel free to add more. (I know Sweave and Knitr are R package but the focus of my question is the layout - what makes it IMO a TeX-question.)
How can I use data from R in a TeX-table and modify the layout of the table? This question is not about how to produce a beautifull nice table. It is about producing exactly the table I show here.
Sweave or Knitr
This is the example.Rnw
file. The table is a example what kind of layout-modifications I want to do. And at the end of the code you see sample-R data. The file can be used via Sweave()
or knit()
.
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\columnwidth}{XX}
\hline\hline
\multicolumn{2}{X}{Should look like this.}\\
\hline\hline
\textbf{Gänge}&\\
\hspace*{5ex}3&15\\
\hspace*{5ex}4&12\\
\hspace*{5ex}5&5\\
\hline
\textbf{Zylinder}&\\
\hspace*{5ex}4&11\\
\hspace*{5ex}6&7\\
\hspace*{5ex}8&14\\
\hline\hline
\end{tabularx}
<<>>=
require(xtable)
table(mtcars$gear)
table(mtcars$cyl)
@
\end{document}
Another approach
Another approach I found is this in R.
df <- data.frame(table(mtcars$cyl))
capture.output(
latex(df, file='')
)
This produces this TeX-code.
[1] "%latex.default(data.frame(table(mtcars$cyl)), file = \"\")%"
[2] "\\begin{table}[!tbp]"
[3] "\\begin{center}"
[4] "\\begin{tabular}{llr}"
[5] "\\hline\\hline"
[6] "\\multicolumn{1}{l}{data.frame}&\\multicolumn{1}{c}{Var1}&\\multicolumn{1}{c}{Freq}\\tabularnewline"
[7] "\\hline"
[8] "1&4&$11$\\tabularnewline"
[9] "2&6&$ 7$\\tabularnewline"
[10] "3&8&$14$\\tabularnewline"
[11] "\\hline"
[12] "\\end{tabular}\\end{center}"
[13] "\\end{table}"
When storing that string in an R-object it could be modified then and used for later output. But it sounds like an unefficent workaround and not as a solution. And I am still wouldn't know how to realize my example table witht that approach.