1

I want to convert a list of tables to Latex using xtable() and create a pdf using Knitr in R-studio.

I have tried to use llply() on the list but it does not work.

Here is a list of tables:

library(plyr)
library(xtable)
Data <- data.frame(a=rbinom(100,1,0.5), b=rbinom(100,1,0.3), c=rbinom(100,1,0.6))
combos <- combn(ncol(Data),2)
TabelFn <- function(x) {
  Table <- addmargins(table(Data[, x[1]], Data[, x[2]]))
  return(Table)
}
Table <- alply(.data=combos, .margins=2, .fun=TabelFn, .expand=TRUE)

Table # list of tables

I tried to run llply() on it:

llply(Table, function(x) {xtable(x)})

And got this output:

$`1`
% latex table generated in R 2.15.3 by xtable 1.7-1 package
% Sat Apr 13 19:45:40 2013
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \hline
 & 0 & 1 & Sum \\ 
  \hline
0 & 48.00 & 11.00 & 59.00 \\ 
  1 & 31.00 & 10.00 & 41.00 \\ 
  Sum & 79.00 & 21.00 & 100.00 \\ 
   \hline
\end{tabular}
\end{table}

$`2`
% latex table generated in R 2.15.3 by xtable 1.7-1 package
% Sat Apr 13 19:45:41 2013
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \hline
 & 0 & 1 & Sum \\ 
  \hline
0 & 27.00 & 32.00 & 59.00 \\ 
  1 & 21.00 & 20.00 & 41.00 \\ 
  Sum & 48.00 & 52.00 & 100.00 \\ 
   \hline
\end{tabular}
\end{table}

$`3`
% latex table generated in R 2.15.3 by xtable 1.7-1 package
% Sat Apr 13 19:45:41 2013
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \hline
 & 0 & 1 & Sum \\ 
  \hline
0 & 40.00 & 39.00 & 79.00 \\ 
  1 & 8.00 & 13.00 & 21.00 \\ 
  Sum & 48.00 & 52.00 & 100.00 \\ 
   \hline
\end{tabular}
\end{table}

But Latex will not accept it. I guess it is because of the list-names like $1 and $2 and so on. This question xtable output for a list of tables explains a way to do it, but I was hoping there was a less complicated way of doing it?

Community
  • 1
  • 1
Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79

1 Answers1

3

I hesitate to post this because it doesn't add much to the linked question that you shared...

Just change llply(...) to l_ply and make sure that the chunk options for the xtables is set to something like <<echo=FALSE, results='asis'>>=

I was able to create an Rnw file with the following content that works just fine:

\documentclass{article}

\begin{document}

<<echo=FALSE>>=
library(plyr)
library(xtable)
Data <- data.frame(a=rbinom(100,1,0.5), b=rbinom(100,1,0.3), c=rbinom(100,1,0.6))
combos <- combn(ncol(Data),2)
TabelFn <- function(x) {
  Table <- addmargins(table(Data[, x[1]], Data[, x[2]]))
  return(Table)
}
Table <- alply(.data=combos, .margins=2, .fun=TabelFn, .expand=TRUE)
@

<<echo=FALSE, results='asis'>>=
l_ply(Table, function(x) { print(xtable(x)) })
@

\end{document}

A couple of points to note:

  • $ is a special character in LaTeX, so having them in your output will create problems
  • alply and llply will show the output, but you don't want that, so you should use the _ versions (a_ply and l_ply) when appropriate. From the help file to l_ply, All output is discarded. This is useful for functions that you are calling purely for their side effects like displaying plots or saving output.
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • Thanks, but I cant get it to work. When i paste your code into an R-sweave document in R-studio, and press "Compile PDF" i just get a blank PDF (it is blank apart from the asdf i added after \begindocument so that it could compile). Can you create a PDF with this? – Rasmus Larsen Apr 13 '13 at 19:48
  • Great idea! It worked with the print() command! I had read on the Knitr page that print() was not necessary, but apparently it still is sometimes :) – Rasmus Larsen Apr 13 '13 at 20:13
  • @RasmusLarsen, `print` is required here by `l_ply` to force the resulting output be be `printed` in a normal `R` session, thus it will be required by `knitr` – mnel Apr 15 '13 at 00:13
  • @mnel, I think my original successes without `print` might have had something to do with files laying around in my directory. Thanks for clarifying. – A5C1D2H2I1M1N2O1R2T1 Apr 15 '13 at 05:42