2

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.

enter image description 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.

  • 1
    Use the R 'knitr' package. Just search this site for 'knitr table' read the questions and answers. You will find your answer. Then post your solution as an answer to your question. – R. Schumacher Dec 1 '16 at 21:41
  • Can you specify that or give me a hint. I only find standard knitr examples/code. I found nothing that modify the layout of a table. – buhtz says get vaccinated Dec 2 '16 at 9:01
1

In my eyes it is not a solution but it works.

I puzzeld the pieces of tex-code on the side of R together into a list of strings and than put it out.

This Rnw file

\documentclass{article}
\usepackage{tabularx}
\begin{document}
<<results='asis', echo=FALSE>>=
funcTabCharacteristics <- function (h, t) {
    rc <- c()

    rc <- c(rc, paste('\\textbf{', h, '}&\\tabularnewline', sep=''))
    for (n in names(t)) {
        rc <- c(rc, paste('\\hspace*{5ex}', n, '&', t[n], '\\tabularnewline', sep=''))

    }

    return (rc)
}

# lines of tex-code
tex <- c()
# table head
tex <- c(tex, '\\begin{tabularx}{\\columnwidth}{XX}')
tex <- c(tex, '\\hline\\hline\\multicolumn{2}{X}{My Table-1}\\tabularnewline')
tex <- c(tex, '\\hline\\hline')
# gear
tex <- c(tex, funcTabCharacteristics('Gänge', table(mtcars$gear)))
tex <- c(tex, '\\hline')
# cyl
tex <- c(tex, funcTabCharacteristics('Zylinder', table(mtcars$cyl)))
tex <- c(tex, '\\hline\\hline')
# table foot
tex <- c(tex, '\\end{tabularx}')
# output the texcode
cat(tex, sep='\n')
@
\end{document}

Isn't there a real elegant solution for humans?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.