Question

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?

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top