Automating LaTeX tables with specifications from R

The simplest suggestion would be to use the xtable R package and the add.to.row argument:

dat <- data.frame(A = 1:10, B = runif(10))
xdat <- xtable(dat)
addtorow <- list(pos = list(seq_len(nrow(dat)-1)),
                 command = "\\hdashline \n")
print(xdat, add.to.row = addtorow)

Here, addtorow$pos is a list containing the indices 1, 2, ..., 9 which means after rows 1, 2, ... 9, include the latex function/command listed in string addtorow$command.

The above code produces:

> print(xdat, add.to.row = addtorow)
% latex table generated in R 2.13.1 by xtable 1.5-6 package
% Fri Aug 19 21:51:21 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
  \hline
 & A & B \\ 
  \hline
1 &   1 & 0.94 \\ 

 \hdashline 
2 &   2 & 0.50 \\ 

 \hdashline 
3 &   3 & 0.86 \\ 

 \hdashline 
4 &   4 & 0.43 \\ 

 \hdashline 
5 &   5 & 0.34 \\ 

 \hdashline 
6 &   6 & 0.41 \\ 

 \hdashline 
7 &   7 & 0.30 \\ 

 \hdashline 
8 &   8 & 0.38 \\ 

 \hdashline 
9 &   9 & 0.74 \\ 

 \hdashline 
10 &  10 & 0.98 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

Take a look at the help for ?xtable and ?print.xtable for info on controlling other options.

Tags:

R

Tables