2

I'm trying to write some R code that takes data from my (evolving) experiment, makes a summary statistics table, and inputs it into the .tex document that I'm writing. I want to avoid copy-pasting. I want to avoid sweave because I don't have the badwidth to learn it right now, and because my code is complicated and takes a long time to run.

What I want is a table like I can get with the tables package in R, exported to a text file that I can then import into the .tex document using the \input{} command. Here is some simple R code that doesn't work like I'd like it to:

1> fac = c(rep("a",10),rep("b",10),rep("c",10))
1> var = c(rnorm(10,mean=4,sd = 5),rnorm(10,mean=2,sd = 10),rnorm(10,mean=3,sd = 1))
1> d = data.frame(fac,var)
1> library(tables)
1> sumstats = with(d, tabular(var*(fac+1)~mean+sd))
1> latex(sumstats)
\begin{tabular}{llcc}
\hline
 & fac & mean & \multicolumn{1}{c}{sd} \\ 
\hline
var & a  & $-0.1415$ & $\phantom{0}7.241$ \\
 & b  & $\phantom{-}0.3324$ & $14.704$ \\
 & c  & $\phantom{-}3.1590$ & $\phantom{0}1.118$ \\
 & All  & $\phantom{-}1.1166$ & $\phantom{0}9.271$ \\
\hline 
\end{tabular}
1> write.table.tabular(latex(sumstats),file='sumstats.txt')
\begin{tabular}{llcc}
\hline
 & fac & mean & \multicolumn{1}{c}{sd} \\ 
\hline
var & a  & $-0.1415$ & $\phantom{0}7.241$ \\
 & b  & $\phantom{-}0.3324$ & $14.704$ \\
 & c  & $\phantom{-}3.1590$ & $\phantom{0}1.118$ \\
 & All  & $\phantom{-}1.1166$ & $\phantom{0}9.271$ \\
\hline 
\end{tabular}
1> read.table('sumstats.txt')
            V1
1 character(0)

Is there a way to get R to write a summary statistics table that had been formatted for latex, that avoids the need to dive into sweave?

1
  • I get an error when I run your code: Error in term2table(rows[[i]], cols[[j]], data, n) : Duplicate values: var and fac
    – vaettchen
    Commented Aug 24, 2014 at 10:49

2 Answers 2

2

Can't run your code but from what I read in your post this should help:

In R:

w <- latex( sumstats, file = "sumstats.tex" )

and then in LaTeX:

\input{sumstats}

assuming that this all takes place in the same folder.

[edited] As stated in the help file:

 To prevent the latex file from being displayed store the result of
 ‘latex’ in an object, e.g. ‘w <- latex(object, file='foo.tex')’.
1
  • Thanks, this works. But it also opens up the table in a graphics device, which stops the rest of the script from running until closed. Any idea how to stop that behavior? Commented Aug 25, 2014 at 5:58
2

You can try other ways of exporting R objects to latex like xtable package. It has a lot more options and gives more control over the type of table exported. A small example : to specify the digits in the statistics reported,

xtable(sumStats, digits = 2) 

and can specify floating environments like longtable etc. Please see documentation and gallery for examples.

1
  • Hi Anusha. Could you try to expand a little bit your answer including more details? Commented Oct 22, 2014 at 6:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .